0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00
logto/packages/core/src/middleware/koa-error-handler.ts

26 lines
530 B
TypeScript
Raw Normal View History

2021-08-30 11:30:54 +08:00
import { RequestErrorBody } from '@logto/schemas';
import { Middleware } from 'koa';
2021-08-30 11:30:54 +08:00
import RequestError from '@/errors/RequestError';
export default function koaErrorHandler<StateT, ContextT, BodyT>(): Middleware<
StateT,
ContextT,
BodyT | RequestErrorBody
> {
return async (ctx, next) => {
try {
await next();
} catch (error: unknown) {
if (error instanceof RequestError) {
ctx.status = error.status;
ctx.body = error.body;
2022-01-27 19:26:34 +08:00
return;
}
throw error;
}
};
}