mirror of
https://github.com/logto-io/logto.git
synced 2025-01-27 21:39:16 -05:00
c8d45a13f0
* test(core): add middleware tests add middleware tests * fix(ut): fix typo fix typo
25 lines
530 B
TypeScript
25 lines
530 B
TypeScript
import { RequestErrorBody } from '@logto/schemas';
|
|
import { Middleware } from 'koa';
|
|
|
|
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;
|
|
|
|
return;
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
};
|
|
}
|