How to catch mutation errors from the server
See original GitHub issueI’ve got a question about error handling client side.
My server returns this on validation error:
{
"data": {
"auth": null
},
"errors": [
{
"message": "An authentication error has occurred",
"name": "AuthenticationError",
"time_thrown": "2017-03-26T01:26:50.439Z",
"data": {
"errors": {
"login": "Authentication failed. Invalid email or password"
}
}
}
]
}
I’d like to get access to the validation errors using apollo-client mutation - something like this:
return auth(values) //<— this is a mutation
.then(({data}) => {
loginThenRedirect(data); // <--- log user in
})
.catch((mutationErrors) => {
const {errors} = mutationErrors.data // <--- doesn't work
this.setState({errors}) // <— display the errors
});
But mutationErrors
get swallowed up and only show a string Error: GraphQL error: An authentication error has occurred…
Is there a way to get the contents of the server error so I can display it client side?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:23
- Comments:15 (3 by maintainers)
Top Results From Across the Web
Resolving a mutation with errors | Lift-off IV - Apollo GraphQL
Let's open up the resolvers.js file again in the server/src folder. ... When the error is thrown, we'll make sure to catch it...
Read more >How to handle mutation GraphQL error instead of Apollo ...
If you want all errors caught like #1, there is no solution. useMutation doesn't make connections to see the 403 response until you...
Read more >Handling GraphQL errors like a champ with unions and ...
Error handling can be frustrating in GraphQL. This post shows you how to use unions and interfaces to handle errors in a more...
Read more >type Mutation - GraphQL Rules
Mutations should return user errors and business logic error immediately in the Payload of the mutation into the field errors . All errors...
Read more >Error Handling | Redux Toolkit
If you need to access the error or success payload immediately after a mutation, you can chain .unwrap() . Using .unwrap. addPost({ id: ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
When I catch the error from a mutation like this,
The following message is logged to the console,
So I got the impression that this issue was still occurring.
When I inspected the object more closely, like this,
It logged an array of error objects.
So the problem was my expectations, not the object itself. Just putting this here for posterity, in case anyone else is caught off-guard by this behaviour.
@helfer Ok that worked out.
I was able to get the error object by doing
const error = JSON.parse(JSON.stringify(mutationError))
not sure if there is a better way to turn the string into an object but this works for me.
Thank you all for the help!