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

Issue getting status code with onError

Open DanceParty opened this issue 5 years ago • 15 comments

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.

screen shot 2018-10-03 at 10 06 45 am

EDIT: I can post more information if need be, but really struggling to figure this out.

DanceParty avatar Oct 03 '18 15:10 DanceParty

I have got the same issue, but on a 413 status code. After some digging on not really closer to a solution.

harm-less avatar Oct 12 '18 11:10 harm-less

@harm-less Yes, same here unfortunately. For now, I just have a generic splash error page for all network issues regardless of HTTP Code

DanceParty avatar Oct 12 '18 17:10 DanceParty

Any update on this?

jpbyrne avatar Dec 19 '18 16:12 jpbyrne

@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?

DanceParty avatar Dec 20 '18 00:12 DanceParty

Thanks @KeevanDance but unfortunately err.response is also undefined.

jpbyrne avatar Jan 16 '19 08:01 jpbyrne

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.

jvandemo avatar Jan 31 '19 15:01 jvandemo

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?

JoviDeCroock avatar Feb 14 '19 16:02 JoviDeCroock

@jvandemo so you were able to actually access the status code? wonder if that would have worked for my use case as well... ?

DanceParty avatar Feb 27 '19 16:02 DanceParty

@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!

smeads avatar Mar 29 '19 20:03 smeads

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 avatar Mar 29 '19 21:03 JoviDeCroock

@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

jjenzz avatar Apr 01 '19 12:04 jjenzz

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.

JoviDeCroock avatar Apr 01 '19 12:04 JoviDeCroock

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

richardmillen94 avatar Dec 17 '19 20:12 richardmillen94

For me, console.log(JSON.stringify(networkError)) shows: {"response":{},"statusCode":401,"bodyText":""}. But networkError.statusCode and networkError.status do not exist.

jjb avatar Jan 14 '20 19:01 jjb

Screenshot from 2020-02-24 19-19-08 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.

danjenson avatar Feb 25 '20 03:02 danjenson