opengenerals/server/middlewares/error-response.mjs
2024-03-06 20:41:04 +08:00

23 lines
472 B
JavaScript

import { InvalidArgumentError } from '@og/error-utils';
/** @typedef {import('koa').Context} Context */
/** @typedef {import('koa').Next} Next */
/**
* @param {Context} ctx
* @param {Next} next
*/
export async function errorAsResponse(ctx, next) {
try {
await next();
} catch (err) {
if (err instanceof InvalidArgumentError) {
ctx.status = 400;
ctx.body = { error: err.toString(), errno: 'EIA' };
} else {
ctx.status = 500;
throw err;
}
}
};