1.0.1 • Published 6 years ago
koa-any-error v1.0.1
koa-any-error
##install
$ npm install koa-any-error --save
##errorFormats
{
json(error,ctx,options){return {}},
html(error,ctx,options){return ''},
text(error,ctx,options){return ''}
}
options
{
accepts:['json', 'html', 'text'],
isDev:true,
method:(error,ctx,options)=>{
}
}
// dependencies
const Koa = require("koa");
const error = require("koa-any-error");
const app = new Koa();
const errorFormats = {
json(err, ctx, opts) {
logger.error(`throw ::: error : ${err.stack || err}`);
const status = err.status || err.statusCode || 500;
const result = {
code: err.code || status,
message: status,
message: err.message,
success: false,
reason: "Unexpected"
};
if (env === "dev") {
result["stack"] = err.stack;
}
return result;
}
};
// middleware
app.use(error(errorFormats, options));
notes
请务必在初始化在 koa 中间件的第一顺位 , 避免不必要问题