lin-mizar
Version:
The core library of Lin CMS
35 lines (34 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.error = void 0;
const http_exception_1 = require("../exception/http-exception");
const extend_1 = require("../extend");
const config_1 = require("../config");
const CodeMessage = config_1.config.getItem('codeMessage', {});
/**
* 全局异常处理中间件
*/
exports.error = (err, ctx) => {
ctx.type = 'application/json';
if (err instanceof http_exception_1.HttpException) {
ctx.status = err.status || 500;
ctx.body = JSON.stringify({
code: err.code,
message: err.message,
request: `${ctx.method} ${ctx.req.url}`
});
}
else {
extend_1.logger.error(err);
if (config_1.config.isDebug()) {
ctx.body = JSON.stringify(err);
}
else {
ctx.body = JSON.stringify({
code: 9999,
message: CodeMessage.getMessage(9999),
request: `${ctx.method} ${ctx.req.url}`
});
}
}
};