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

36 lines
884 B
TypeScript
Raw Normal View History

import RequestError from '@/errors/RequestError';
import { RequestErrorBody } from '@logto/schemas';
import decamelize from 'decamelize';
import { Middleware } from 'koa';
import { errors } from 'oidc-provider';
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;
}
if (error instanceof errors.OIDCProviderError) {
ctx.status = error.status;
ctx.body = {
message: error.error_description ?? error.message,
code: `oidc.${decamelize(error.name)}`,
data: error.error_detail,
};
return;
}
throw error;
}
};
}