Further explanation of `didEncounterErrors` on extending additional properties on errors
See original GitHub issueIn the documentation (https://www.apollographql.com/docs/apollo-server/data/errors/), if I need additional properties, it suggests using didEncounterErrors
lifecycle hook to add additional properties.
consider using the didEncounterErrors lifecycle hook to attach additional properties to the error
I am having a hard time figuring out how to actually extend the info because the function signature for formatError
prop is (error: GraphQLError) => GraphQLFormattedError
. I would like the formatError
prop function to have things like request
and context
as well.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:27
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Can't always get extensions property of error in ...
Hi, I want to capture errors in the didEncounterErrors hook and persist only some type of errors to my logging service while ignoring...
Read more >How to Only Catch non-ApolloError Errors with Apollo ...
I have an Apollo GraphQL server, where I want to only report internal server errors (not errors extending ApolloError like ...
Read more >Custom errors, extending Error - The Modern JavaScript Tutorial
Let's make a more concrete class PropertyRequiredError , exactly for absent properties. It will carry additional information about the property ...
Read more >Apollo GraphQL Plugin All in One - E.Y. - Medium
Whenever an error occurs, the didEncounterErrors event fires and the ... fires even if the GraphQL operation encounters one or more errors.
Read more >API Reference: @apollo/gateway - Apollo Federation
On startup, the gateway throws an error if any of these requests fail. ... response objects contains additional HTTP-specific properties, such as headers...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For those of you wondering, this is how I am currently handling the errors:
Before I go into error handling logic with apollo, just wanted to preface with how I handle error in my app in general.
There are 2 different categories of errors: known/unknown. Known errors are errors that I throw purposefully, and would like to get returned back to the user (to show in the UI, such as form errors like invalid password). Unknown errors are errors that I don’t want to expose or expect (such as database query errors), and I want to convert to generic message like “something went wrong” and also want reported back to me via reporting system (I use Sentry).
I currently filter out the “expected” from “unexpected” using an array of
knownErrors
which consist of errors I know I will throw from the app.And this is how I handle them with apollo server.
tl;dr:
didEncounterErrors
hook =>formatError
propLonger version:
When an error occurs (any type of “thrown” error whether intentional or not), it will be first caught by
graphqlErrorHandler
plugin usingdidEncounterErrors
hook, and then finally go throughformatError
function which gets returned as a response back to the user.In order to access request/response bodies, I put them inside my graphql context object as
req
andres
.Code example:
apolloServer.ts
graphqlErrorHandler.ts
I also have the same problem and I also decided to use the small hack proposed above. It is weird that it is not a native support for it…