apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

statusCode is not defined on networkRequest in runtime

Open Oikio opened this issue 3 years ago • 1 comments

Intended outcome: When server responds with 401:

onError(({networkRequest}) => {
  networkRequest.statusCode // should be defined and contain actual statusCode
})

Documentation link that mentions it https://www.apollographql.com/docs/react/networking/advanced-http-networking/#customizing-response-logic

Actual outcome: statusCode is not defined on networkRequest in runtime in some cases, see reproduction section.

How to reproduce the issue: Run query against endpoint which returns 401 status code. In our case it's POST request and empty body response. Latter seems to be the issue. So that instead of being server error, networkError has different shape.

Update: looks like if response is empty TypeError is returned instead of ServerError.

Versions

 System:
    OS: macOS 12.1
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 3.2.0 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  Browsers:
    Chrome: 102.0.5005.61
    Safari: 15.2
  npmPackages:
    @apollo/client: ^3.6.7 => 3.6.8 

Oikio avatar Jun 13 '22 12:06 Oikio

I am also seeing this issue with the same version of apollo client. The status code field is just not present

jmtimko5 avatar Aug 30 '22 22:08 jmtimko5

Hi folks 👋🏻 network errors are always tricky on the client side because servers are always finding creative ways to subvert our expectations 😛

That said, I'm wondering if maybe there's a typo somewhere? In your code I see that you're destructuring networkRequest. In the docs you linked, however, the example shows networkError.

In case that's not the issue here and we're looking at networkError, I wanted to note that this is an optional property. Furthermore, it's a union type:

https://github.com/apollographql/apollo-client/blob/f0cc25299eecb1990669716db77649dc25cf046f/src/errors/index.ts#L42

Where Error is just a JavaScript error. So statusCode won't necessarily be reported depending on the type of error being returned.

I hope that helps narrow things down - let me know if you still need support here 🙏🏻

bignimbus avatar Feb 14 '23 04:02 bignimbus

We're closing this issue now but feel free to ping the maintainers or open a new issue if you still need support. Thank you!

github-actions[bot] avatar Mar 16 '23 05:03 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.

github-actions[bot] avatar Apr 16 '23 00:04 github-actions[bot]