tachybase_todo/packages/plugins/error-handler/src/error-handler.ts
chenos 8bf23004a1
refactor: middleware (#857)
* refactor: middleware

* fix: test error

* fix: test error

* fix: test

* fix: tag
2022-09-29 21:05:31 +08:00

43 lines
814 B
TypeScript

export class ErrorHandler {
handlers = [];
register(guard: (err) => boolean, render: (err, ctx) => void) {
this.handlers.push({
guard,
render,
});
}
defaultHandler(err, ctx) {
ctx.status = err.statusCode || err.status || 500;
ctx.body = {
errors: [
{
message: err.message,
code: err.code,
},
],
};
if (ctx.status === 500) {
console.error(err);
}
}
middleware() {
const self = this;
return async function errorHandler(ctx, next) {
try {
await next();
} catch (err) {
for (const handler of self.handlers) {
if (handler.guard(err)) {
return handler.render(err, ctx);
}
}
self.defaultHandler(err, ctx);
}
};
}
}