MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
945 stars 278 forks source link

Consider just using fetch for requests instead of got #534

Closed RavenHursT closed 2 years ago

RavenHursT commented 2 years ago

I kept seeing a 422 Unprocessable Entity error getting thrown from the striptTags API while using this lib.

So I drilled down into the source and saw that the lib being used to make requests is got (first I've ever heard of got BTW).

Well, was trying to find the reason behind the 422 response and found that got throws on non 2xx/3xx response codes, but worse than that, it just swallows the response object, and only throws a custom HTTPError object w/ just two properties:

https://github.com/sindresorhus/got/blob/4f24bd11fb50757ec8753a74e4a7b18de95ba155/source/core/errors.ts#L97-L101

So when I was trying to introspect the body of the response, I was out of luck, there's no way to get ahold of that in a catch scope when all that's passed in is the HTTPError.

I had to manually construct the request and fire it off in an HTTP client, just so I could find out what was actually wrong.

Screen Shot 2022-04-11 at 5 13 21 PM

This is beyond frustrating. Either find a way to give the consumer of this a way to get a handle on the response object, or else just use fetch and return the native Response object. Then at least we'd be able to read the body stream and parse it ourselves. What isn't acceptable is for a lib to just swallow useful data just because a certain span of response codes were returned.

lpinca commented 2 years ago

Well, was trying to find the reason behind the 422 response and found that got throws on non 2xx/3xx response codes, but worse than that, it just swallows the response object, and only throws a custom HTTPError object w/ just two properties:

The info you are looking for is in error.response.body where error is the error you are receiving. There are multiple closed issues about this. Feel free to open a PR to document it.