question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to catch mutation errors from the server

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:23
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

57reactions
variousauthorscommented, Aug 23, 2017

When I catch the error from a mutation like this,

.catch((e) => { console.log(e) })

The following message is logged to the console,

Error: GraphQL error: Unauthorized
    at new ApolloError (ApolloError.js:32)
    at QueryManager.js:119
    at <anonymous>

So I got the impression that this issue was still occurring.

When I inspected the object more closely, like this,

.catch((e) => { console.log(e.graphQLErrors) })

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.

24reactions
kojukacommented, Mar 30, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found