-
Notifications
You must be signed in to change notification settings - Fork 344
apollo-link-error return error to caller #1022
Comments
I have the same issue in my app. I want to catch all errors in error link (for example so I could log them) but I also want to forward them to the original caller and handle them over there as they might contain a custom error msg that needs to be displayed to the user. My code:
Note that calling |
@lbrdar Have you found any workaround for this? |
Nope 😞 |
@lbrdar Isn't the way to do this is with a catch ? loginMutation({ variables: { data } })
.then(({ data, errors }) => {
if (errors) {
// I WANT TO BE ABLE TO READ ERROR MESSAGE HERE SO I CAN DISPLAY IT TO THE USER
} else if (data) {
....
}
}).catch(errors => {
}); |
@Nosherwan I just installed version 1.1.11 and everything works fine now... You may also check your inner promises (maybe somewhere you forgot to return a promise inside other promise) |
@merksam thank you for the comment. const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path }) => {
console.log(`[GraphQL error]: ${message}, Location: ${locations}, Path: ${path}`);
},
);
}
}); In the above code block, the graphQLErrors are caught and something can be done in the onError handler. However those errors are not passed to the actual calling promise. |
Have someone found any workaround for this? |
@Nosherwan @prem-prakash Could we contact somewhere in chat or even make a call so we try to find out why it's working in my case and don't in yours? (if it's still relevant) |
I found the solution, kind of weird, but that it is: This prints just the message updateProfile: function() {
this.$apollo
.mutate({
mutation: UPDATE_PROFILE,
variables: current_user
})
.catch((error) => {
console.log("this prints just the message", error);
});
} BUT this prints the full content of errors updateProfile: function() {
this.$apollo
.mutate({
mutation: UPDATE_PROFILE,
variables: current_user
})
.catch(({ graphQLErrors }) => {
console.log("this prints the full content of 'errors'", graphQLErrors);
});
} I think this issue can be closed |
what about adding this to the documentation? |
Sorry but solution above doesn't work. I guess a full example on how sending back the graphqlErrors in link-error back to the initial caller. In my case, when there is an error, the following ApolloQueryResult object doesn't get the error details consoled in the onError. Some in the try ... catch surrounding the call. Can not get the graphqlError details from the server. Just a "400 error"..
server config:
|
@prem-prakash thanks.... savior.. |
This problem is extremely persistent. I have no control over the graphql server on my project, only the client. The server is responding with graphql errors in the correct shape, but with a 400 status code. In the error-link I have access to Is there anyone out there who can successfully handle a 400 status code response with error messaging, and pass that messaging to the UI at the component that called the mutation? |
Ok well I have a pretty unreasonable approach, but it is working: I am setting a boolean
Then in a MutationError component, I query the cache for presence of error and the error message, conditionally render the gql error or a real network error:
This will be global since I need it on all mutations since my server is always returning 400 for what should be 200 + graphql errors (I'm a little salty about that)... So the important thing here, is that on every component mutation, I use an empty onError callback which prevents an unhandled exception from Apollo, and on success I must remember to reset the hasGraphError boolean in the cache:
Then the mutation error component takes the useMutation error as props (which both allows real network errors to be determined, and makes sure that we are not rendering the global cached gql error on the wrong component):
As I said, this approach is pretty unreasonable, however it is currently working to solve:
Issues with this approach as coded: this just writes the last GQL error in the array to the cache, so it is not supporting multiple GQL errors at the same time. This should be fairly easy to manage though, by buildling an array of errors, and storing that in the cache, but you'll have to define a local schema/resolvers to do this, or potentially just JSON.stringify it to store in the cache as a string. You'll also need to clear the currentGraphError in the cache on success, rather than just setting the boolean to false. Hope it helps someone! |
Also, in case it helps, note that errorPolicy currently does not work on useMutation hooks, this is a bug, and was recently addressed in this PR: apollographql/apollo-client#5863 , but has not made it to release at this time. |
Any progress on this from the developers? |
So I am using Apollo-link-error to manage auth and admin global errors. But if I use it, my catch methods in my promises do not return the errors.
It looks as if all the errors go through apollo-link-error and they are not passed back to the caller method.
Is there a way to return the error to the caller so I can manage some errors locally and some errors globally?
The text was updated successfully, but these errors were encountered: