Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {
} = require('./lib/utils');

module.exports = app => {
app.config.coreMiddleware.push('onerrorBizHandler');

// logging error
const config = app.config.onerror;
const viewTemplate = fs.readFileSync(config.templatePath, 'utf8');
Expand Down
57 changes: 57 additions & 0 deletions app/middleware/onerror_biz_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const { EggError, ErrorType } = require('egg-errors');
const { accepts } = require('./lib/utils');

module.exports = (_, app) => {
const biz = app.config.onerror.biz;

return async function onerrorBizHandler(ctx, next) {
try {
await next();
} catch (err) {
const fn = app.config.onerror.accepts || accepts;
const contentType = fn(ctx);

const type = EggError.getType(err);
switch (type) {
case ErrorType.ERROR: {
ctx.status = err.status || 500;
ctx.body = format(err, contentType);
break;
}

case ErrorType.EXCEPTION: {
ctx.logger.error(err);
err.status = 500;
ctx.body = format(err, contentType);
break;
}

case ErrorType.BUILTIN:
default:
throw err;
}
}

function format(err, contentType) {
if (contentType === 'json' && biz.formatJSON) {
ctx.body = biz.formatJSON(err);
if (contentType === 'text' && biz.formatText) {
ctx.body = biz.formatText(err);
if (contentType === 'html' && biz.formatHtml) {
ctx.body = biz.formatHtml(err);
} else {
ctx.body = defaultFormat(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultFormat 直接交给 koa-onerror?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接 throw 出来吗?

}
}
};

};

function defaultFormat(err) {
return {
code: err.code,
message: err.message,
};
}
11 changes: 11 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ exports.onerror = {
appErrorFilter: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的 // 5xx error will redirect to ${errorPageUrl} 注释都需要修改一下了。

// default template path
templatePath: path.join(__dirname, '../lib/onerror_page.mustache'),
// normalize your error response from error object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrorType.ERROR and ErrorType.EXCEPTION

biz: {
formatJSON: null,
formatText: null,
formatHtml: null,
},
system: {
formatJSON: null,
formatText: null,
formatHtml: null,
},
};