Socket
Socket
Sign inDemoInstall

koa-any-error

Package Overview
Dependencies
2
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-any-error


Version published
Weekly downloads
6
increased by100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 08 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc