koa-any-error

1.0.1 • Public • Published

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 中间件的第一顺位 , 避免不必要问题

Readme

Keywords

Package Sidebar

Install

npm i koa-any-error

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

5.68 kB

Total Files

5

Last publish

Collaborators

  • lynn.wang