mirror of
https://github.com/logto-io/logto.git
synced 2025-02-03 21:48:55 -05:00
23 lines
484 B
TypeScript
23 lines
484 B
TypeScript
|
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;
|
||
|
}
|
||
|
};
|
||
|
}
|