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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@airquality/custom-error

v1.0.0

Published

Custom error

Downloads

5

Readme

@airquality/custom-error

自定义错误。可统一设置错误码、错误信息和用户自定义属性。

安装

npm i --save @airquality/custom-error

使用

此模块导出一个 CustomError 构造器和一个 createError 函数。

CustomError

  • new CustomError([code[, params]]) 实例化
  • CustomError.set(errors) 配置错误信息
  • CustomError.set(code, message) 配置错误信息
  • CustomError.set(code, properties) 配置错误信息
  • CustomError.get([code]) 得到错误信息

在实例化之前,应该先配置统一的错误信息,方法维护。有三种方法可以配置错误信息。

const CustomError = require('@airquality/custom-error')

// 1. 通过一个错误信息对象数组进行配置,每个对象中都必须包含 code 属性,
//    message 属性是可选的,它可以是一个字符串模板,在实例化时可通过传
//    入 params 参数编译成最终展示的信息。
CustomError.set([

  // code 属性是必须的
  { code: 'ERR_CUSTOM_ERROR1', message: 'message' },

  // 可传入字符串模板
  { code: 'ERR_CUSTOM_ERROR2', message: 'error message ${message}' },

  // 可忽略 message 参数,并添加用户自定义的属性
  { code: 'ERR_CUSTOM_ERROR3', otherProp: 'other prop' },
])

// 2. 通过传入 code 和 message 字符串进行配置
CustomError.set('ERR_CUSTOM_ERROR4', 'error message')

// 3. 通过传入 code 字符串和 props 对象进行配置
CustomError.set('ERR_CUSTOM_ERROR5', {
  message: 'error message',
  otherProp: 'other prop',
})

配置错误信息后,可通过 get 方法得到这些信息。

// 根据 code 得到某一条错误信息
// => { code: 'ERR_CUSTOM_ERROR1', message: 'message' }
CustomError.get('ERR_CUSTOM_ERROR1')

// 不传入 code 参数,可得到全部的错误信息(数组)
CustomError.get()

实例化。

// 不传入任何的参数
const customError1 = new CustomError()

// 传入已经配置的 code,输出的错误信息时 code 对应的 message
const customError2 = new CustomError('ERR_CUSTOM_ERROR1')

// 传入的 code 如果没有在配置中,则输出的错误信息就是 code
const customError3 = new CustomError('Non-existent code will be treated as message')

// 可传入 params 参数,解析字符串模板
// 下面输出的错误信息应该是:'error message test'
const customError4 = new CustomError('ERR_CUSTOM_ERROR2', { message: 'test' })

createError(name[, errors])

返回一个构造器,这个构造器的实例的名称前缀为用户输入的 name 参数。传入 errors 数组,可配置错误信息,其具体使用方法与 CustomError.set(errors) 相同。关于构造器的使用方法,与上面的 CustomError 相同。

const { createError } = require('@airquality/custom-error')
const UserError = createError('UserError', [
  { code: 'ERR_CUSTOM_ERROR', message: 'message' },
])

// ...

许可

MIT.