-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add biz handler #29
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
57916a5
f0cd1b4
7169690
166e5f2
11619a7
0ebbae5
4c6b87a
3bb19e6
d9ab559
8e18cfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const { EggError, ErrorType } = require('egg-errors'); | ||
const { accepts } = require('../../lib/utils'); | ||
|
||
module.exports = (_, app) => { | ||
const application = app.config.onerror.application; | ||
|
||
return async function onerrorBizHandler(ctx, next) { | ||
try { | ||
await next(); | ||
} catch (err) { | ||
const fn = app.config.onerror.accepts || accepts; | ||
const contentType = fn(ctx, 'html', 'text', 'json'); | ||
console.info(contentType); | ||
const type = EggError.getType(err); | ||
console.log(type); | ||
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' && application.formatJSON) { | ||
ctx.body = application.formatJSON(err); | ||
} else if (contentType === 'text' && application.formatText) { | ||
ctx.body = application.formatText(err); | ||
} else if (contentType === 'html' && application.formatHtml) { | ||
ctx.body = application.formatHtml(err); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
}; | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,10 @@ exports.onerror = { | |
appErrorFilter: null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上面的 |
||
// default template path | ||
templatePath: path.join(__dirname, '../lib/onerror_page.mustache'), | ||
// normalize your error response from error object | ||
|
||
application: { | ||
formatJSON: null, | ||
formatText: null, | ||
formatHtml: null, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const { Controller } = require('egg'); | ||
const { EggError, EggException } = require('egg-errors'); | ||
|
||
|
||
class HomeController extends Controller { | ||
async throwError() { | ||
throw new Error('error'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个还是不要改原生了?就作为 BUILDIN 错误好了 |
||
} | ||
|
||
async throwEggError() { | ||
throw new EggError('egg error'); | ||
} | ||
|
||
async throwEggException() { | ||
throw new EggException('egg exception'); | ||
} | ||
|
||
} | ||
|
||
module.exports = HomeController; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.get('/error', app.controller.home.throwError); | ||
app.get('/eggerror', app.controller.home.throwEggError); | ||
app.post('/eggexception', app.controller.home.throwEggException); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
exports.logger = { | ||
level: 'NONE', | ||
consoleLevel: 'NONE', | ||
}; | ||
|
||
exports.keys = 'foo,bar'; | ||
|
||
exports.security = { | ||
csrf: false, | ||
}; | ||
|
||
exports.onerror = { | ||
accepts(ctx, ...args) { | ||
console.log(ctx.type, ctx.headers); | ||
const a = ctx.accepts(...args); | ||
console.log(a, ...args); | ||
return a; | ||
}, | ||
application: { | ||
formatJSON(err) { | ||
return { | ||
message: err.message, | ||
code: err.code, | ||
status: err.status, | ||
data: err.data, | ||
}; | ||
}, | ||
formatText(err) { | ||
return err.message; | ||
}, | ||
formatHtml(err) { | ||
return '<h2>' + err.message + '</h2>'; | ||
}, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "onerror" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.