Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

error link does not give a real error object, thus no networkError.statusCode access possible #218

Closed
miracle2k opened this issue Nov 7, 2017 · 15 comments

Comments

@miracle2k
Copy link

I am basically using this example from the docs ("Afterware"): https://www.apollographql.com/docs/react/basics/network-layer.html

import ApolloClient from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { onError } from 'apollo-link-error'
import { logout } from './logout';
const httpLink = new HttpLink({ uri: '/graphql' });
const logoutLink = onError(({ networkError }) => {
  if (networkError.statusCode === 401) logout();
})
const client = new ApolloClient({
  link: logoutLink.concat(httpLink),
});

This doesn't work for me. "networkError" seems to be raised as a string, and only tells me "Failed to fetch". It doesn't have a stack, nor the .statusCode property.

https://www.webpackbin.com/bins/-KyLJ0NyjpkxU0ggok70

@maurodelazeri
Copy link

+1

the way I'm solving this temporarily is by picking up the status directly on the component that makes the call

componentWillReceiveProps(nextProps) {
console.log(
"life cycle: componentWillReceiveProps",
nextProps.data.networkStatus
);
}

ex:
7 = 200
8 = 401

@leethree
Copy link

Yes, the networkError object is not very helpful and the response object is missing. There's no way to handle HTTP status codes in network layer.

@tjpeden
Copy link

tjpeden commented Nov 20, 2017

This is kind of a big deal, I haven't found a solution for angular

@jbaxleyiii
Copy link
Contributor

@miracle2k we are working to improve errors with #244, does it look like it will fix your issue?

@miracle2k
Copy link
Author

miracle2k commented Nov 21, 2017

@jbaxleyiii Not sure, but a quick scan of the patch set doesn't lead me to conclude that it changes the object that networkError is; if you look at the webpackbin link I put above, this seems to be a string, which means we don't know the status code.

Specifically, I want the status code so I can handle "403 Not Authorized" responses from the server and redirect to a login screen. This seems impossible right now.

@tjpeden
Copy link

tjpeden commented Nov 22, 2017

@miracle2k Looks like they added test coverage around 401 (which is what I think you mean) so hopefully that means the things they changed actually produce a result containing the status code.

I would like to point out, however, that your webpackbin demonstrates (for me at least) that networkError.statusCode is undefined, not a string. That behavior is consistent with my findings as well though. I have hope that this patch fixes that issue.

@yuricamara
Copy link

The problem and the solution (I guess)

export interface ErrorResponse {
  graphQLErrors?: GraphQLError[];
  networkError?: Error;
  response?: ExecutionResult;
  operation: Operation;
}

from

export interface ErrorResponse {

Error in "networkError?: Error" should be like HttpErrorResponse from Angular

A workaround

With Angular

   import { HttpErrorResponse } from '@angular/common/http';	

    const errorLink = onError(({ networkError }) => {
      const networkErrorRef:HttpErrorResponse = networkError as HttpErrorResponse;
      if (networkErrorRef && networkErrorRef.status === 401) {
        console.log('Going to login page...');
      }
    });

Without Angular

Create the type HttpErrorResponse like the one from Angular or change it for 'any'

const errorLink = onError(({ networkError }) => {
  const networkErrorRef:HttpErrorResponse = networkError as HttpErrorResponse;
  if (networkErrorRef && networkErrorRef.status === 401) {
    console.log('Going to login page...');
  }
}); 

@jbaxleyiii
Copy link
Contributor

This should be fixed in the newest release thanks to @evans work!

@goldenbearkin
Copy link

seems it hasn't fixed.

@miracle2k
Copy link
Author

Same here. I updated the webpackbin above with the latest releases to show that the statusCode is still not accessible.

@webmobiles
Copy link

yes, please waiting the fix, I've upgraded but have this problem in the real production environment..

@bogdansoare
Copy link

Also having this issue.

@akrigline
Copy link

Likewise. @jbaxleyiii It doesn't look like the latest npm releases have this in.

@webmobiles
Copy link

apollo looks a project abandoned, i'm migrating to manual fetch with javscript

@evans
Copy link
Contributor

evans commented Feb 15, 2018

@webmobiles I'm sorry you feel this way! I hope that we can work together to fix this now

Let's move the conversation to the open issue #300

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests