Skip to content

Commit

Permalink
Better default error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
serdnam committed Feb 23, 2022
1 parent 153b2de commit f65615b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}))

Expand Down

0 comments on commit f65615b

Please sign in to comment.