Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can networkerror be catched first #6626

Closed
wukong1995 opened this issue Jul 17, 2020 · 7 comments
Closed

Can networkerror be catched first #6626

wukong1995 opened this issue Jul 17, 2020 · 7 comments

Comments

@wukong1995
Copy link

when I set http code is 401, and throw new error in server. In chrome network, I get http code is 401 and response data is:

{
  data: {operationName: null},
  errors: [{xxxerror}]
}

see,https://github.com/apollographql/apollo-client/blob/master/src/link/http/createHttpLink.ts#L142-L170, I get graphqlError not networkError, I can't get 401.

@wukong1995 wukong1995 changed the title Can networkerror be captured first Can networkerror be catched first Jul 17, 2020
@maapteh
Copy link

maapteh commented Jul 17, 2020

You can catch a 401 also on the client part itself. In link field you can use OnError

import { onError } from '@apollo/client/link/error';

return new ApolloClient({
        ...SNIP...
        link: ApolloLink.from([
            onError(({ graphQLErrors, networkError }) => {
                if (graphQLErrors)
                    graphQLErrors.map(({ message, locations, path }) =>
                        console.log(
                            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
                        ),
                    );
                if (networkError)
                    console.log(`[Network error]: ${networkError}`);
            }),
            split(
                (operation: any) => operation.getContext().important === true,
                httpLink as any, // if the test is true -- debatch
                batchHttpLink as any, // otherwise, batching is fine
            ),
        ]),
        ...SNIP...
    });

@wukong1995
Copy link
Author

wukong1995 commented Jul 17, 2020

@maapteh my code has it. But, in createHttpLink.ts, it call observer.next(err.result);, not observer.error(err). because data isn't null. when it call next, status code will be lost.

@maapteh
Copy link

maapteh commented Jul 17, 2020

I think its related to #6612 because the error is lost in SSR calls.

For client side calls i indeed not get the error (in link), but i do get it on component .
For server-side calls i do get the error (in link), but i dont get it on component.

{"graphQLErrors":[{"message":"must authenticate","locations":[{"line":2,"column":3}],"path":["authenticationError"],"extensions":{"code":"UNAUTHENTICATED"}}],"networkError":null,"message":"must authenticate"}

i added a page to my sandbox repo to show your scenario on it as well

@wukong1995
Copy link
Author

wukong1995 commented Jul 17, 2020

@maapteh my scenario is in client side. In my application, server is nodejs.

// server
ctx.status = 403
throw new Error('no authenticate')

then, i will get graphQLErrors in onError.

In my app, it has 401, 403, 405 and has same error message. so i can't distinguish errors based on error message.

@maapteh
Copy link

maapteh commented Jul 17, 2020

instead of AuthenticationError, did you play with ApolloError? ApolloError accepts errorCode (string).

        bad: () => {
            console.log('[server] GraphQL server query: bad');
            throw new ApolloError('oeps', '401');
        },
        authenticationError: () => {
            throw new AuthenticationError('must authenticate');
        },

@wukong1995
Copy link
Author

wukong1995 commented Jul 17, 2020

@maapteh got it. I will try. thanks.

@wukong1995
Copy link
Author

it can solve my problem. i will close issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
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

2 participants