From ed1fbeba72cbdbb3fe69559f6595b9ed162a52b1 Mon Sep 17 00:00:00 2001 From: Kristiyan Tachev Date: Wed, 27 Mar 2024 20:58:41 +0200 Subject: [PATCH] feat(fastify): added global error handler on module init in order to be set on a runtime level --- packages/fastify/src/fastify.module.ts | 3 +++ packages/fastify/src/fastify.tokens.ts | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/packages/fastify/src/fastify.module.ts b/packages/fastify/src/fastify.module.ts index 2e224a9..6a833b3 100644 --- a/packages/fastify/src/fastify.module.ts +++ b/packages/fastify/src/fastify.module.ts @@ -25,6 +25,9 @@ export class FastifyModule { for (const schema of schemas) { instance.addSchema(schema); } + if (options.globalErrorHandler) { + instance.setErrorHandler(options.globalErrorHandler(instance)); + } return instance; }, }, diff --git a/packages/fastify/src/fastify.tokens.ts b/packages/fastify/src/fastify.tokens.ts index bf941ea..7bde774 100644 --- a/packages/fastify/src/fastify.tokens.ts +++ b/packages/fastify/src/fastify.tokens.ts @@ -1,9 +1,12 @@ import { InjectionToken } from '@rhtml/di'; import { + FastifyError, FastifyHttpOptions, FastifyInstance, FastifyPluginCallback, FastifyRegisterOptions, + FastifyReply, + FastifyRequest, FastifySchema, } from 'fastify'; @@ -17,4 +20,11 @@ export interface FastifyPlugin { export interface FastifyModuleOptions extends FastifyHttpOptions { plugins: FastifyPlugin[]; schemas: FastifySchema[]; + globalErrorHandler: ( + instance: FastifyInstance + ) => ( + error: FastifyError, + request: FastifyRequest, + reply: FastifyReply + ) => FastifyReply; }