Skip to content

Commit 3348acf

Browse files
committed
feat: Added preHandler option.
1 parent ecdef72 commit 3348acf

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Register as a plugin, optional providing any of the following options:
2626
- `convertResponsesValidationErrors`: Convert response validation errors to a structured human readable object. Default is to enable when `NODE_ENV` environment variable is different from `production`.
2727
- `allowUndeclaredResponses`: When converting response validation errors, allow responses that have no schema defined instead of throwing an error.
2828
- `responseValidatorCustomizer`: A function that receives a Ajv instances before compiling all response schemas. This can be used to add custom keywords, formats and so on.
29+
- `preHandler`: A function invoked before the error handlers that can modify the original error thrown. The function must accept the error as first argument and return the error to process.
2930

3031
Once registered, the server will use the plugin handlers for all errors (basically, both `setErrorHandler` and `setNotFoundHandler` are called).
3132

src/handlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export function handleValidationError(error: FastifyError, request: FastifyReque
3636
}
3737

3838
export function handleErrors(error: FastifyError | Error, request: FastifyRequest, reply: FastifyReply): void {
39+
if (request[kHttpErrorsEnhancedConfiguration]?.preHandler) {
40+
error = request[kHttpErrorsEnhancedConfiguration]!.preHandler!(error)
41+
}
42+
3943
// It is a generic error, handle it
4044
const code = (error as NodeError).code
4145

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export const plugin = fastifyPlugin(
2121
hideUnhandledErrors: options.hideUnhandledErrors ?? isProduction,
2222
convertValidationErrors: options.convertValidationErrors ?? true,
2323
responseValidatorCustomizer: options.responseValidatorCustomizer,
24-
allowUndeclaredResponses: options.allowUndeclaredResponses ?? false
24+
allowUndeclaredResponses: options.allowUndeclaredResponses ?? false,
25+
preHandler: typeof options.preHandler === 'function' ? options.preHandler : undefined
2526
}
2627

2728
instance.decorate(kHttpErrorsEnhancedConfiguration, null)

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Ajv, { ValidateFunction } from 'ajv'
2+
import { FastifyError } from 'fastify'
23

34
export const kHttpErrorsEnhancedConfiguration = Symbol('fastify-http-errors-enhanced-configuration')
45
export const kHttpErrorsEnhancedResponseValidations = Symbol('fastify-http-errors-enhanced-response-validation')
@@ -8,6 +9,7 @@ export interface Configuration {
89
convertValidationErrors?: boolean
910
allowUndeclaredResponses?: boolean
1011
responseValidatorCustomizer?: (ajv: Ajv) => void
12+
preHandler?: (error: FastifyError | Error) => Error
1113
}
1214

1315
declare module 'fastify' {

0 commit comments

Comments
 (0)