Skip to content

Commit

Permalink
refactor: simplify error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
L2jLiga committed Dec 21, 2023
1 parent 1f4810b commit 7236899
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
30 changes: 11 additions & 19 deletions lib/decorators/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,19 @@ export function ErrorHandler<T extends ErrorConstructor>(
target: Value | This,
ctx: ClassMethodDecoratorContext<This, Value> | ClassFieldDecoratorContext<This, Value> | 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));
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rollup-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 7236899

Please sign in to comment.