apollo-link
apollo-link copied to clipboard
Issue getting status code with onError
Expected Behavior To be able to access the HTTP Status Code allowing me to handle a redirect to an error screen for failed login
Actual Behavior statusCode is undefined on networkError giving me no way to check what the server status code is.
Code Snippet
//Apollo Client
const httpLink = new HttpLink({ uri: `${getServerUrl()}/graphql`, credentials: 'same-origin' });
const logoutLink = onError(({ networkError }) => {
console.log('apollo error:', networkError.statusCode);
});
const client = new ApolloClient({
link: logoutLink.concat(httpLink),
cache: new InMemoryCache(),
});
Also, if there is anyway to catch the 401 using Apollo Boost that would be my preference, but my assumption is that I need to use the more configurable Apollo Client.

EDIT: I can post more information if need be, but really struggling to figure this out.
I have got the same issue, but on a 413 status code. After some digging on not really closer to a solution.
@harm-less Yes, same here unfortunately. For now, I just have a generic splash error page for all network issues regardless of HTTP Code
Any update on this?
@jpbyrne I gave up, but came across something similar very recently with a different library, and was solved by accessing the object like err.response
instead of just err
. Maybe try console.logging err.response
instead?
Thanks @KeevanDance but unfortunately err.response
is also undefined
.
In our Angular application, using apollo-link version 1.2.6, networkError
is an instance of HttpErrorResponse
with the status available as networkError.status
instead of networkError.statusCode
.
So the conclusion of this issue is that the typescript types are wrong? We are expecting to find the statusCode under error.statusCode but instead it is found under status
.
Am I understanding this right?
@jvandemo so you were able to actually access the status code? wonder if that would have worked for my use case as well... ?
@JoviDeCroock thanks for all the work you do on this project and others.
I apologize if I missed a resolution elsewhere for the issue below. I figured this would be a good place to ask about it as it seems to be related.
I'm currently using version 1.1.10
of apollo-link-error
. I'm using TypeScript (3.3.4) and running into a type error with regards to networkError
.
Property 'statusCode' does not exist on type 'Error | ServerError | ServerParseError'. Property 'statusCode' does not exist on type 'Error'.ts(2339)
My use case is as follows:
onError(({ networkError }) => {
if (
networkError &&
networkError.statusCode &&
networkError.statusCode === 401
) {
Do Something....
}
}),
I see the ErrorResponse
interface uses ServerError
as a type for the networkError
field which has statusCode
on it, but doesn't seem to carry over.
If there is a solution I missed somewhere or suggestions on how to resolve this typing issue I'd appreciate them.
Thank you!
Hmm it's odd that the type isn't being carried over. Just to be sure, when you check what's on networkError. statusCode is on it? as a tempfix you could retype it but I'll try to look at it asap.
Have been really busy and I am just catching up on issues right now.
@JoviDeCroock: It doesn't appear to be a TypeScript issue. I can't access networkError.status
or networkError.statusCode
when using apollo-link-error
: https://codesandbox.io/s/04n430xkxw
No issue here: https://codesandbox.io/s/ry49xr9nzm
Just open console and look at the error under "network", yours is a preflight error which is a whole other beast.
Since the type of the error here is Error | ServerError | ServerParseError
, due of the nature of unions, only the shared fields within Error
exist on the type (ServerError
and ServerParseError
are both intersections of Error
). Typescript allows you to narrow the types that the error instance might be by using the in
operator e.g.
if (networkError && 'statusCode' in networkError) { ... }
would narrow the type down to ServerError | ServerParseError
for the scope of the if statement or...
if (networkError && 'result' in networkError) { ... }
would narrow it just to ServerError
.
So this isn't necessarily an issue with the module as this kind of type checking is a valid solution but union types used it this way may tend to confuse people (myself included).
For me, console.log(JSON.stringify(networkError))
shows: {"response":{},"statusCode":401,"bodyText":""}
. But networkError.statusCode
and networkError.status
do not exist.
I'm getting 401s but the networkError object doesn't resemble any of those in the documentation: https://www.apollographql.com/docs/link/links/http/#errors. I'm using apollo-link-error 1.1.12.