Error Masking
Mesh Gateway will forward all errors (resolvers and sources) to the end client by default.
This page will guide you in masking the errors and puts the proper error handling practices in your
additionalResolvers
.
Enabling Errors masking
To enable error masking on your Mesh Gateway:
Configure the useMaskedErrors()
Envelop plugin by
adding something like the following;
sources:
# …
transforms:
# …
plugins:
- maskedErrors:
errorMessage: 'Something went wrong.'
The maskedErrors
plugin should always put last in the plugin list (example: to avoid masking
errors to the useSentry()
plugin.
Custom error formatting programmatically
You can also provide plugins programmatically.
sources:
# …
transforms:
# …
additionalEnvelopPlugins: ./envelopPlugins.ts
Envelop allows you to provide your error formatting function as follows:
import { GraphQLError } from 'graphql'
import { PluginOrDisabledPlugin, useMaskedErrors } from '@envelop/core'
export const formatError: FormatErrorHandler = err => {
if (err.originalError && err.originalError instanceof GraphQLError === false) {
return new GraphQLError('Sorry, something went wrong.')
}
return err
}
const plugins: PluginOrDisabledPlugin = [
useMaskedErrors({
errorMessage: 'Something went wrong.',
formatError
})
]
export default plugins
This can be helpful, for example, to provide different error types based on a Source.
Custom resolvers error handling
All errors will be hidden from the end-user when error masking is enabled.
However, some might want to throw specific visible errors.
To make an error visible, leverage the GraphQLError
error class as follows:
import { GraphQLError } from 'graphql'
import { Resolvers } from './.mesh'
const resolvers: Resolvers = {
Book: {
author: {
selectionSet: /* GraphQL */ `
{
authorId
}
`,
async resolve(root, _args, context, info) {
const result = await context.Authors.Query.authors_v1_AuthorsService_GetAuthor({
root,
args: {
input: {
id: root.authorId
}
},
context,
info
})
if (!result) {
throw new GraphQLError({
message: `Author with id '${root.authorId}' not found.`,
extensions: {
code: 'AUTHOR_NOT_FOUND',
// complex values
bookId: root.id,
authorId: root.authorId
}
})
}
return result
}
}
}
}