From f65615b2a473e8da090c96de5b70aed7e5a89353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20M=C3=A1rmol?= Date: Wed, 23 Feb 2022 08:59:17 -0400 Subject: [PATCH] Better default error handler --- README.md | 4 +--- index.ts | 13 ++++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 388abc8..9d34cfa 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ The `onResponse` hook is not supported. When an error occurs, the error handler set by this plugin captures the error with `Sentry.captureException` and then executes a Promise-returning function returned by `errorHandlerFactory`, passing it the error, request, and reply parameters and setting `this` to the Fastify instance (as if it were called by Fastify itself). -If no function is provided as an argument, a default async function which logs the error message, sets the status code to 500 and returns +If no function is provided as an argument, a default async function which logs the error message and then returns either the error itself if the status code is not 500, or the following if the status code is 500: ```json { @@ -193,8 +193,6 @@ If no function is provided as an argument, a default async function which logs t } ``` -to the client is used. - * `closeTimeout` This parameter is passed as-is to the `Sentry.close()` method, see the [Sentry documentation](https://docs.sentry.io/platforms/node/configuration/draining/) for more information about this method. diff --git a/index.ts b/index.ts index 025f0e4..ab6b7bc 100644 --- a/index.ts +++ b/index.ts @@ -147,11 +147,14 @@ function validateConfiguration(options: FastifySentryOptions): CleanOptions { opts.errorHandlerFactory = opts.errorHandlerFactory ?? ( () => (async function defaultErrorHandlerFactory(err, req, rep) { - req.log.error(err.message) - rep.status(500) - return { - error: 500, - message: 'Internal server error' + rep.log.error(err) + if (rep.statusCode === 500) { + return { + error: 500, + message: 'Internal server error' + } + } else { + return err } }))