apollo-client: Global error handling
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!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 26 (4 by maintainers)
I did something along these lines:
Obviously showUserFeedback is implemented somewhere… Hope this helps!
@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:
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.
I just released a library that solves this problem for Apollo 2: react-apollo-network-status
Maybe this is helpful to you 🙂
@jazzdragon can you please share the whole implementation? I don’t understand what you mean with “showUserFeedback is implemented somewhere”
I agree with @machineghost. Even some example code would help.
you can create a custom network interface to handle this: http://dev.apollodata.com/core/network.html#custom-network-interface
you can wrap the what apollo client network interface you are using then add in your error handling in there.