0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-03 21:48:55 -05:00
logto/packages/core/src/middleware/koa-error-handler.ts

23 lines
484 B
TypeScript
Raw Normal View History

import RequestError, { RequestErrorBody } from '@/errors/RequestError';
import { Middleware } from 'koa';
export default function koaErrorHandler<StateT, ContextT>(): Middleware<
StateT,
ContextT,
RequestErrorBody
> {
return async (ctx, next) => {
try {
await next();
} catch (error: unknown) {
if (error instanceof RequestError) {
ctx.status = error.status;
ctx.body = error.body;
return;
}
throw error;
}
};
}