got
got copied to clipboard
Throw more useful errors for request validation
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.
I knew someone would raise an issue about this someday... See https://github.com/sindresorhus/is/issues/107
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.
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.
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.
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.
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`.
In my case, a string was being passed as
timeout. As suggested in the original comment, an error indicating that thetimeoutoption was of the incorrect type would be very helpful.
Thank you, bro 😎
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.
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
}