got icon indicating copy to clipboard operation
got copied to clipboard

Throw more useful errors for request validation

Open alechirsch opened this issue 5 years ago • 11 comments
trafficstars

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.

alechirsch avatar May 12 '20 21:05 alechirsch

I knew someone would raise an issue about this someday... See https://github.com/sindresorhus/is/issues/107

szmarczak avatar May 12 '20 22:05 szmarczak

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 avatar Jul 08 '21 18:07 stephenmathieson

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.

stephenmathieson avatar Jul 08 '21 18:07 stephenmathieson

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.

movy avatar Jan 08 '22 15:01 movy

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.

binarymist avatar Jan 31 '22 03:01 binarymist

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`.

rinogo avatar Jun 01 '22 22:06 rinogo

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 😎

ryanhssn avatar Aug 31 '22 09:08 ryanhssn

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.

guillenotfound avatar Mar 29 '23 13:03 guillenotfound

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
	}

movy avatar Oct 22 '23 07:10 movy