apollo-client: [4.0] Question about new error types
Hi,
I’ve been testing the new Apollo Client v4 in an enterprise Angular app, and I want to start by saying great work so far on the new changes, to name a few:
- The migration of Observables to RxJS, espcially for Angular, as it provides a much more seamless intergration.
watchQueryobservable no longer terminating on errors.- More consistent typing across the library.
On the other hand, one area that I found a bit more challenging to migrate was error handling, and I had some feedback if you don’t mind me sharing:
Default error message
Currently, it’s common to use apollo.watchQuery in a component and simply display result.error.message. I noticed the error message has changed, for example:
v3:
Email address is already in use
v4:
The GraphQL server returned with errors:
- Email address is already in use
My guess, and I might be wrong, most developers would probably choose to hide that part of the message, for example in our case, almost all of our end users don’t know what GraphQL is and that to me feels like an implementation detail.
I know, the solution is simple, to call a formatError function, or implement onError link and wrap it in a new error type with a formatted message, but that introduces overhead and complexity in the app, for what I believe should be the default behavior. I’m keen to hear your thoughts on this?
Network errors
I noticed ApolloError has been replaced by multiple new error types, for the most part it’s a welcome change.
The only challenge I found with the new types is that it’s hard to distinguish a network error from other errors thrown in the app, this is especially challenging when an ApolloClient operation is only one part of a more complex async flow, it used to be easy to do an ApolloError.networkError check then, for example, decide to retry the operation. But now, and I might be missing something here, there’s no simple reliable way of doing that anymore? I’d appreciate some suggestions.
Thanks.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (17 by maintainers)
@wassim-k appreciate that feedback! That was indeed an issue. I added a test for that to ensure its not caught and found a much better place to add that logic.
We just released those changes in
4.0.0-alpha.10so please try this out and let us know if you find any issues with it!By the way, we went with the name
LinkErrorinstead ofNetworkErrorwhich better represents what those errors are. Its possible for errors not related to the network to be emitted from the link chain, so that name was a bit of a misnomer.I believe the latest alpha addresses all the feedback from this issue so I’m going to go ahead and close this. Once again, thanks so much for providing it! Majorly helpful in getting to a nicer resolution here.
Please feel free to open new issues if you discover bugs with the new updates or if you’d like to provide additional feedback. Thanks!
Awesome! I’ll add this to the v4 milestone so we can track it as a docs task. Thanks!
Ok thanks for confirming!
One of the reasons we stopped wrapping errors in v4 was because these error types obscured the source of the error. Worse, “ApolloError” as a name ended up in a lot of error reporting tools, so at a glance it looked like the client was doing something wrong and it was difficult to tell that there might have been user error involved. We felt it better to surface the raw error so that its easier to see the cause of an issue in those error reporting tools rather than obscuring it through a wrapped instance.
I won’t lie, I think we’d prefer to keep it this way for that reason. I believe it will be easier to understand the cause of an error by leaving them unwrapped. That said, it should be easy to create your own custom link that wraps these errors with your own class which you can use to differentiate:
How would you feel if we provided this guidance in the docs vs adding something to Apollo Client itself?
@wassim-k I just opened https://github.com/apollographql/apollo-client/pull/12557 which updates the error message for
CombinedGraphQLErrorsandCombinedProtocolErrorsback to the v3 format. This PR also makes it possible to override the default formatter in case you want to change this app-wide. That should provide enough flexibility for those that may want to change the format without having to create their own wrapped classes. We will try and get this out in the next alpha or two.Thanks again for the feedback!