coa-error
TypeScript icon, indicating that this package has built-in type declarations

1.5.2 • Public • Published

coa-error

GitHub license npm version npm downloads PRs Welcome

COA框架基础错误类,用于统一化错误提示

安装

yarn add coa-error

使用示例

import { CoaError } from 'coa-error'


// 定义并抛出一个新的错误
throw new CoaError('User.UserAgeInvaild','用户年龄错误')

// 使用静态方法抛出(可以当做一个语法糖)
CoaError.throw('User.UserAgeInvaild','用户年龄错误')

// 也可以使用message方法,仅仅提示不在stdio显示(由coa上层框架控制,框架外部调用等同于throw)
CoaError.message('User.UserAgeInvaild','用户年龄错误')

数据结构

COA错误要求必须定义如下统一参数:

  • code 错误代码,
  • message 错误消息

类定义如下

class CoaError extends Error {
  name = 'CoaError'

  code: string
  stdout: boolean

  constructor (code: string, message: string, stdout: boolean = true) {
    super(message)
    this.code = code
    this.stdout = stdout
  }

  static message (code: string, message: string): never {
    throw new CoaError(code, message, false)
  }

  static throw (code: string, message: string): never {
    throw new CoaError(code, message)
  }
}

Package Sidebar

Install

npm i coa-error

Weekly Downloads

4

Version

1.5.2

License

MIT

Unpacked Size

5.88 kB

Total Files

9

Last publish

Collaborators

  • adaex