From 7236899b68255a030f6f799a8eafbdb033aab369 Mon Sep 17 00:00:00 2001 From: Andrey Chalkin Date: Thu, 21 Dec 2023 19:36:23 +0200 Subject: [PATCH] refactor: simplify error handler --- lib/decorators/error-handler.ts | 30 +++++++++++------------------- lib/rollup-config.js | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/decorators/error-handler.ts b/lib/decorators/error-handler.ts index 876ab325..6b26bc85 100644 --- a/lib/decorators/error-handler.ts +++ b/lib/decorators/error-handler.ts @@ -44,27 +44,19 @@ export function ErrorHandler( target: Value | This, ctx: ClassMethodDecoratorContext | ClassFieldDecoratorContext | string | symbol, ) => void { - return function (target, handlerName) { - if (typeof handlerName === 'object' && 'kind' in handlerName) { - const container = getErrorHandlerContainerMetadata(handlerName.metadata); + return function (target, handlerNameOrContext) { + const isEsmContext = typeof handlerNameOrContext === 'object' && 'kind' in handlerNameOrContext; + const container = isEsmContext + ? getErrorHandlerContainerMetadata(handlerNameOrContext.metadata) + : getErrorHandlerContainer((target as abstract new () => unknown).constructor); + const handlerName = isEsmContext ? handlerNameOrContext.name : handlerNameOrContext; - if (parameter == null) { - container.push(handlerFactory(() => true, handlerName.name)); - } else if (typeof parameter === 'string') { - container.push(handlerFactory((error?: ErrorWithCode) => error?.code === parameter, handlerName.name)); - } else { - container.push(handlerFactory((error?: Error) => error instanceof parameter, handlerName.name)); - } + if (parameter == null) { + container.push(handlerFactory(() => true, handlerName)); + } else if (typeof parameter === 'string') { + container.push(handlerFactory((error?: ErrorWithCode) => error?.code === parameter, handlerName)); } else { - const container = getErrorHandlerContainer((target as abstract new () => unknown).constructor); - - if (parameter == null) { - container.push(handlerFactory(() => true, handlerName)); - } else if (typeof parameter === 'string') { - container.push(handlerFactory((error?: ErrorWithCode) => error?.code === parameter, handlerName)); - } else { - container.push(handlerFactory((error?: Error) => error instanceof parameter, handlerName)); - } + container.push(handlerFactory((error?: Error) => error instanceof parameter, handlerName)); } }; } diff --git a/lib/rollup-config.js b/lib/rollup-config.js index 420ff67f..fff0cfb6 100644 --- a/lib/rollup-config.js +++ b/lib/rollup-config.js @@ -11,6 +11,6 @@ export default { entryFileNames: () => '[name].cjs', }, plugins: [typescript({ tsconfig: './tsconfig.lib.json', moduleResolution: 'Node' })], - external: ['fastify', 'fastify-plugin', 'node:fs'], + external: ['fastify', 'fastify-plugin', 'node:fs', 'node:fs/promises'], treeshake: false, };