Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw more useful errors for request validation #1257

Open
alechirsch opened this issue May 12, 2020 · 11 comments
Open

Throw more useful errors for request validation #1257

alechirsch opened this issue May 12, 2020 · 11 comments
Labels
enhancement This change will extend Got features external The issue related to an external project ✭ help wanted ✭

Comments

@alechirsch
Copy link

alechirsch commented May 12, 2020

What problem are you trying to solve?

I passed in searchParams option as null and got back an error that looks like this:

TypeError: Expected value which is `predicate returns truthy for any value`, received value of type `Array`.

It took some time to figure out why I was getting this error, I had to look at the callstack of the error figure out what was wrong.

Would it be possible to add to the error message saying that the option searchParams (or another option) is the wrong type? It would be much clearer and help with debugging. Another option is to accept null for options, which seems reasonable to me.

@szmarczak
Copy link
Collaborator

I knew someone would raise an issue about this someday... See sindresorhus/is#107

@szmarczak szmarczak added enhancement This change will extend Got features external The issue related to an external project ✭ help wanted ✭ labels May 12, 2020
@stephenmathieson
Copy link

Currently dealing with this.

Error log
{
  "error": "Expected value which is `predicate returns truthy for any value`, received values of types `string`.",
  "stack": "TypeError: Expected value which is `predicate returns truthy for any value`, received values of types `string`.\n at assertType (/usr/src/app/packages/server/node_modules/@sindresorhus/is/dist/index.js:293:15)\n at Object.any (/usr/src/app/packages/server/node_modules/@sindresorhus/is/dist/index.js:386:16)\n at normalizeArguments (/usr/src/app/packages/server/node_modules/got/dist/source/core/index.js:386:21)\n at mergeOptions (/usr/src/app/packages/server/node_modules/got/dist/source/create.js:39:25)\n at Function.got.extend (/usr/src/app/packages/server/node_modules/got/dist/source/create.js:151:22)\n at new REDACTED (/usr/src/app/packages/server/node_modules/_REDACTED_/dist/index.js:31:38)\n at boot (/usr/src/app/packages/server/dist/app.js:180:22)\n at Object.<anonymous> (/usr/src/app/packages/server/dist/app.js:239:9)\n at Module._compile (internal/modules/cjs/loader.js:1085:14)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
  "level": "error",
  "message": "startup error"
}
Relevant LOCs
assert(this.options.token, 'Token required')
assert(this.options.url, 'URL required')
const prefixUrl = new URL(this.options.url)
this.request = got.extend({
  prefixUrl,
  timeout: this.options.timeout || DEFAULT_TIMEOUT,
  headers: {
    Authorization: `Bearer ${this.options.token}`
  }
})

Would hugely appreciate it if got was more helpful here.

@stephenmathieson
Copy link

In my case, a string was being passed as timeout. As suggested in the original comment, an error indicating that the timeout option was of the incorrect type would be very helpful.

@movy
Copy link

movy commented Jan 8, 2022

In my case I was migrating from axios and had to change {body: data} to {json: data} in requests. A clearer description which request property is problematic would've been helpful.

@binarymist
Copy link

binarymist commented Jan 31, 2022

I also spent a long time debugging an issue (after upgrading to 12.0.1) where by the looks of it options.json no longer accepts a json string.

I was getting:

Expected value which is `predicate returns truthy for any value`, received values of types `string`

There was no mention of this in the Breaking Changes, at least not that I could fathom.

@rinogo
Copy link

rinogo commented Jun 1, 2022

GAH! Thanks, @movy. That appears to have been my exact problem!

I was confused because a quick glance at the docs for Options make like body is a suitable property for making things happen.

Alas, as you've pointed out, the fix was as simple as using json instead of body. Perhaps the docs for body could be improved to make mention of json.

Of course, arguably the best solution would be to improve the error messages. With that said, here's my error message to help with Google indexing:

RequestError: Expected value which is `predicate returns truthy for any value`, received values of types `Object`.

@ryanhssn
Copy link

In my case, a string was being passed as timeout. As suggested in the original comment, an error indicating that the timeout option was of the incorrect type would be very helpful.

Thank you, bro 😎

Cruikshanks added a commit to DEFRA/water-abstraction-system that referenced this issue Jan 18, 2023
https://eaflood.atlassian.net/browse/WATER-3833

When we deployed the lastest version of the repo with the new [Request new bill run in Charging Module API](#83) we kept getting a `500` error returned by the service.

The root error was

```text
Error: undefined - Expected value which is `predicate returns truthy for any value`, received values of types `Object`.
6|system  |               at _formattedInitiateBillingBatchError (/home/repos/water-abstraction-system/app/controllers/bill-runs/bill-runs.controller.js:40:15)
6|system  |               at create (/home/repos/water-abstraction-system/app/controllers/bill-runs/bill-runs.controller.js:25:12)
6|system  |               at processTicksAndRejections (node:internal/process/task_queues:96:5)
6|system  |               at async exports.Manager.execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
6|system  |               at async Object.internals.handler (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/handler.js:46:20)
6|system  |               at async exports.execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/handler.js:31:20)
6|system  |               at async Request._lifecycle (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:370:32)
6|system  |               at async Request._execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:280:9)
```

After some research we found mention of this error in a couple of [Got](https://github.com/sindresorhus/got) related issues

- [Throw more useful errors for request validation](sindresorhus/got#1257)
- [Enhance assert error messages](sindresorhus/is#107 (comment))

Basically, we were passing in an invalid property for one of the options. In our case we were passing `body: {}` when it should have been `json: {}`.

So, just a simple tweak but it gets the thing working!
Cruikshanks added a commit to DEFRA/water-abstraction-system that referenced this issue Jan 19, 2023
https://eaflood.atlassian.net/browse/WATER-3833

When we deployed the latest version of the repo with the new [Request new bill run in Charging Module API](#83) we kept getting a `500` error returned by the service.

The root error was

```text
Error: undefined - Expected value which is `predicate returns truthy for any value`, received values of types `Object`.
6|system  |               at _formattedInitiateBillingBatchError (/home/repos/water-abstraction-system/app/controllers/bill-runs/bill-runs.controller.js:40:15)
6|system  |               at create (/home/repos/water-abstraction-system/app/controllers/bill-runs/bill-runs.controller.js:25:12)
6|system  |               at processTicksAndRejections (node:internal/process/task_queues:96:5)
6|system  |               at async exports.Manager.execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
6|system  |               at async Object.internals.handler (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/handler.js:46:20)
6|system  |               at async exports.execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/handler.js:31:20)
6|system  |               at async Request._lifecycle (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:370:32)
6|system  |               at async Request._execute (/home/repos/water-abstraction-system/node_modules/@hapi/hapi/lib/request.js:280:9)
```

After some research, we found mention of this error in a couple of [Got](https://github.com/sindresorhus/got) related issues

- [Throw more useful errors for request validation](sindresorhus/got#1257)
- [Enhance assert error messages](sindresorhus/is#107 (comment))

Basically, we were passing in an invalid property for one of the options. In our case we were passing `body: {}` when it should have been `json: {}`.

So, just a simple tweak but it gets the thing working!
@guillenotfound
Copy link

Also facing this issue when trying to post FormData object, any hints on how to fix it? I'm using body to perform the POST request.

@movy
Copy link

movy commented Oct 22, 2023

Coming back here nearly 2 years later.
Experiencing this error with a new project that uses got. This time, timeout is expected to be an object, not a number, i.e.:

timeout: {
		lookup: 100,
		connect: 50,
		secureConnect: 50,
		socket: 1000,
		send: 10000,
		response: 1000
	}

@Jermont25
Copy link

I want to add that I got the errorExpected value, which isplain object, received value of type number`` when trying to do a get. After a lot of research I found that it was because since version 12 of got, the timeout should be added like this:

timeout: {
      request: 5000,
    },

Doc: https://github.com/sindresorhus/got/blob/c8902ba3420b2def5aab68b3f889039469d0b1a8/documentation/6-timeout.md

Blog: https://schalkneethling.com/posts/moving-from-got-11-to-12

@ledlamp
Copy link

ledlamp commented Jul 6, 2024

I set options.timeout = {request: 3000}; and got "RequestError: Expected values which are number or undefined. Received values of type Function.". Wtf? There's no function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features external The issue related to an external project ✭ help wanted ✭
Projects
None yet
Development

No branches or pull requests

10 participants