Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

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

Goal

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).

Problem

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

Attempt

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.

Question

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 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...