Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add biz handler #29

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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);
popomore marked this conversation as resolved.
Show resolved Hide resolved
err.status = 500;
ctx.body = format(err, contentType);
break;
}

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

function format(err, contentType) {
if (contentType === 'json' && biz.formatJSON) {
popomore marked this conversation as resolved.
Show resolved Hide resolved
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: {
popomore marked this conversation as resolved.
Show resolved Hide resolved
formatJSON: null,
formatText: null,
formatHtml: null,
},
};