Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

javascript - Is there a better way of forwarding custom errors in apollo-client w/ apollo-link-error?


Using Apollo, I want to intercept all GraphQL server-response errors; do something w/ each error (like run each through a centralized error-service); and then forward a custom error to the feature that made the original server-request (really, I just want to be able to communicate some metadata to the feature —?it doesn't necessarily have to be a custom Error instance).


Apollo doesn't seem to support the forwarding of custom errors.


I set up an error-link to:

  • intercept each GraphQL error
  • run it through some centralized error-service
  • transform the error into a custom error object w/ extra metadata
  • serialize the custom error object into a JSON string
  • overwrite the message property of the first element of the response.errors w/ the serialized error

Then, I can parse the JSON back into a custom error instance whenever I need it further downstream.

This works fine for the Apollo hooks but breaks for the methods on the apolloClient instance that you get from useApolloClient() —?apolloClient.mutate() and apolloClient.query(). Specifically, the apolloClient methods overwrite my custom error string w/ the original server-error's message.

You can see this issue for more details on the implementation.


Is there a better way? Can't imagine that this isn't a common use-case.

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers


37.1k users