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.

Global error handling

See original GitHub issue

Hi, In my application, as is quite common, I handle errors coming back from the server in the same global way, throughout the app. Right now, every time I make a mutation to the server, I need to include error handling code every time, which is just boilerplate overhead. In my specific case, I use react with redux, and dispatch an action on error to display a snackbar. So, every mutation has this same added code:

mutate(...).catch(err => {
        const error = err.graphQLErrors[0].message
        props.showUserFeedback(error)
      })

where I need to connect the component to redux just for that action, and so forth.

I propose some kind of global error handling in the apollo client settings, maybe with a different way to handle each kind of error, and that could be overridden.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:26 (4 by maintainers)

github_iconTop GitHub Comments

27reactions
jazzdragoncommented, Aug 24, 2017

I did something along these lines:

const networkInterface = createNetworkInterface({uri})

const handleErrors = ({ response }, next) => {
  // clone response so we can turn it into json independently
  const res = response.clone()
  // if it's not user error, we skip this afterware (for example a 401)
  if (!res.ok) {
    // handle network errors based on res.status here, for example:
    if (res.status === 500) {
      showUserFeedback('internal server error')
    }
    return next()
  }

  // handle apollo errors
  res.json().then(json => {
    each(json.data, data => {
      if (data && data.errors && data.errors.length) {
        showUserFeedback(data.errors[0])
      }
    })
    next()
  })
}

networkInterface.useAfter([{
  applyAfterware: handleErrors,
}])

const client = new ApolloClient({
  networkInterface,
})

Obviously showUserFeedback is implemented somewhere… Hope this helps!

15reactions
machineghostcommented, Jul 23, 2017

@jazzdragon you’re not the only one to have missed that. The documentation could really use a “global error handling” section or something; currently the only way a user can find out how to handle errors globally in Apollo is to:

  1. Read all of the documentation front-to-back until the find the “secret” feature in the network interface
  2. Find this ticket
  3. Be a dev 😉

Far more likely they’ll discover the local error handling and think that’s all Apollo offers, because that’s all that’s highlighted in the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global Error Handling in ASP.NET Core Web API - Code Maze
NET Core. In this article, we are going to handle errors by using a try-catch block first and then rewrite our code by...
Read more >
Global Error Handling in ASP.NET Web API 2 - Microsoft Learn
Global exception handling and logging should be services that can run during production and be plugged into existing monitoring solutions (for ...
Read more >
NET 6.0 - Global Error Handler Tutorial with Example
The global error handler is used catch all errors and remove the need for duplicated error handling code throughout the .NET api.
Read more >
Global Exception Handling in .NET 6 | Syncfusion Blogs
Global exception handling with custom middleware grants the developer much broader authority and enhances the procedure. It's a block of code ...
Read more >
Global Exception Handler - UiPath Documentation Portal
The Global Exception Handler is a type of workflow designed to determine the project's behavior when encountering an execution error.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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 Hashnode Post

No results found