npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deepexi-onerror

v0.2.0

Published

koa-onerror middleware eggjs configurer for deepexi framework

Downloads

33

Readme

deepexi-onerror

NPM version npm download Build Status codecov

deepexi-onerror is a koa-onerror middleware eggjs configurer for deepexi framework.

How To

安装依赖

$ npm i deepexi-onerror

通过configurer配置你的onerror中间件

// config.default.js
const onerror = require('deepexi-onerror');
config.onerror = onerror();

更多配置说明

config.onerror = onerror({
    stack: {
        out: true,  // 强制输出堆栈信息,即使是生产环境下
    },
    status: {
      fail: [ 400, 401, 406 ],  // 哪些错误码被认为是业务异常,支持数值、数组、正则、函数
      error: {
          '500': 'System Error'
      }, // 系统异常对应展示的错误信息
    }
})

异常处理规则

分为两类异常:业务异常和系统异常

业务异常

默认400, 401, 406等状态码被视为业务异常(可通过配置修改)。业务异常可以指定code,如果不指定默认为-1。业务异常具备以下特点:

  1. 一般是代码逻辑正确,但由于其它原因导致的操作失败,如接口调用参数错误,业务校验失败(例如新增的用户重名)等
  2. 业务异常的信息可以展示给前端用户看
  3. 每个业务异常会关联错误码(建议唯一),方便追溯

下面代码显示如何抛出一个业务异常

const err = new Error('biz err');
err.status = 406;
// err.unsafeStatus = 430;  // 效果等同status,不同的是status只支持标准的HTTP状态码,而unsafeStatus可以让你使用非标准的HTTP状态码(如430)
err.code = 'DO-999';
throw err;

业务异常抛出后会被处理为以下格式

{
    "success": false,
    "message": "biz err",
    "code": "DO-999"
}

系统异常

其它状态码为系统异常,如果不指定,则默认处理为500。系统异常具备以下特点:

  1. 一般意味着服务端代码错误
  2. 具体错误信息不会展示给前端用户
  3. 非生产环境会有堆栈信息返回,方便问题排查

下面代码显示如何抛出一个系统异常

throw new Error('system err');

系统异常抛出后会被处理为以下格式

{
    "success": false,
    "message": "Internal Server Error",
    "code": -2,
    "stack": "..."
}