coajs / coa-error

Basic Error Library for coajs

Home Page:https://npmjs.org/coa-error

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

coa-error

GitHub license npm version npm downloads PRs Welcome

English | 简体中文

Basic Error Library for coajs, used to unify error messages

Install

yarn add coa-error

Example

import { CoaError } from 'coa-error'

// Define and throw a new error
throw new CoaError('User.UserAgeInvaild', 'User age error')

// Thrown using a static method (can be used as a syntax sugar)
CoaError.throw('User.UserAgeInvaild', 'User age error')

// You can also use the message method, which only prompts but does not display in stdio (controlled by the coa upsteam framework, other calls are equivalent to throw)
CoaError.message('User.UserAgeInvaild', 'User age error')

Data structure

coa error requires that the following uniform parameters must be defined:

  • code error code
  • message error message

The class is defined as follows:

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)
  }
}

About

Basic Error Library for coajs

https://npmjs.org/coa-error

License:MIT License


Languages

Language:TypeScript 100.0%