graphql-error-tracking-extension
This GraphQL extension for Apollo Server 2 adds two functionalities:
- It helps you to log the request context in case of an Exception is happening in your GraphQL API. A trace-id will additionally help you to find the corresponding log entries.
- You can define which Exception types you do reveal the real error to the client or just want to return a generic "internal server error"
Example log using this extension:
[dded97442947d] {"content-type":"application/json","cache-control":"no-cache","postman-token":"c824faa8-0287-406a-b326-bdbb173ee30d","authorization":"***","user-agent":"PostmanRuntime/7.6.0","accept":"*/*","host":"localhost:8080","accept-encoding":"gzip, deflate","content-length":"103","connection":"keep-alive"}
[dded97442947d] Syntax Error: Expected Name, found {
[dded97442947d] Original error body: query {getSetting(settingsId:"123", language: "en") { features { {onPremises} }}
In this case you can easily see that the Exception was just caused by a syntax error. However, if you need to find a real bug the corresponding query can be crucial.
Usage
- This package needs
graphql-extensions
to be installed:npm i graphql-extensions --save
- Install the npm package as a dependency
npm i graphql-error-tracking-extension --save
- Add this extension and the request to the GraphQL context like this:
;; const server = schema extensions: request: req ;
Configuration
The GraphQLErrorTrackingExtension class takes an optional
configuration object new GraphQLErrorTrackingExtension(config)
.
maskHeaders
Replaces http headers with sensitive information with '***'.
Default: ['authorization']
revealErrorTypes
Define which error types (classes) should be revealed to the client. Default is to reveal all original errors to the client. If you set this option, all errors not in the list will be mapped to an Internal Server Error before sending a response to the client. Also have a look to the already available error types defined by Apollo Server 2 link.
Default: null
Example
;; const server = schema extensions: revealErrorTypes: SyntaxError UserInputError AuthenticationError ForbiddenError request: req ;
Important: the array takes the JS classes, not strings!
onUnrevealedError
If you have defined revealErrorTypes
, this callback gets called if an error was mapped to Internal Server Error.
Whatever you do in this callback, the Internal Server Error is send to the client, but you can use it to e.g. forward
this unexpected error to another monitoring system.
Default: null
Example
;;;const errorReporting = ; const server = schema extensions: revealErrorTypes: SyntaxError UserInputError AuthenticationError ForbiddenError { if originalError errorReporting; else errorReporting; } request: req ;