infinitered / apisauce

Axios + standardized errors + request/response transforms.
MIT License
2.78k stars 184 forks source link

Cannot read properties of undefined (reading 'status') #295

Closed stesvis closed 2 years ago

stesvis commented 2 years ago

When the response is ERR_CONNECTION_REFUSED (in my case, server down), every API call throws an exception. My understanding was that we shouldn't need to wrap calls inside a try/catch block, but instead an exception is still thrown.

Is there a solution or workaround, to avoid refactoring all the code to handle api exceptions?

Version: "apisauce": "^2.1.5",

Here's the full error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status') at getProblemFromError (apisauce.js:1:1) at apisauce.js:1:1 at apisauce.js:1:1 at Object.next (apisauce.js:1:1) at apisauce.js:1:1 at new Promise () at __awaiter (apisauce.js:1:1) at N (apisauce.js:1:1) at apisauce.js:1:1 at apisauce.js:1:1

jamonholmgren commented 2 years ago

Yeah, this is a bug. Could you help us track down where we're calling .status on an undefined object in this case, and send in a PR?

stesvis commented 2 years ago

Yeah, this is a bug. Could you help us track down where we're calling .status on an undefined object in this case, and send in a PR?

@jamonholmgren could the problem be here? https://github.com/infinitered/apisauce/blob/master/lib/apisauce.ts#L86

This:

  if (!error.code) return getProblemFromStatus(error.response.status)

Maybe it should be like this?

  if (!error.code) return getProblemFromStatus(error.response ? error.response.status : null)
markholland commented 2 years ago

We're also seeing this after upgrading from 2.1.1 to 2.1.5. Assuming it was introduced during the removal of ramda.js in 2.1.4 which appears to have involved a non-trivial amount of changes.

treeduship commented 2 years ago

I've got a PR ready to go whenever someone has time to look at it.

infinitered-circleci commented 2 years ago

:tada: This issue has been resolved in version 2.1.6 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

stesvis commented 1 year ago

🎉 This issue has been resolved in version 2.1.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

Hi I have upgraded to v2.1.6 but I am still experiences exceptions not being caught when the backend fails to respond to a HTTP request:

TypeError: Cannot read properties of undefined (reading 'config') at apisauce.js:1:1 at apisauce.js:1:1 at Object.next (apisauce.js:1:1) at apisauce.js:1:1 at new Promise () at __awaiter (apisauce.js:1:1) at N (apisauce.js:1:1) at apisauce.js:1:1 at apisauce.js:1:1 at Object.next (apisauce.js:1:1)

Is this the same issue, just happening in another part of the code?

jamonholmgren commented 1 year ago

@stesvis Interesting ... could be another part of the code. The backtrace isn't all that useful there.