错误

一个 Sumor Cloud 工具。
更多文档 一个错误处理库,支持多语言、预定义错误代码和传递数据。

CI Test Coverage Audit

安装

npm i @sumor/error --save

先决条件

Node.JS 版本

需要 Node.JS 版本 18.x 或以上

需要 Node.JS ES 模块

由于此包是用 ES 模块编写的,请在您的 package.json 文件中更改以下代码:

{
  "type": "module"
}

用法

定义错误

import defineError from '@sumor/error'

const MyError = defineError({
  code: {
    USER_NOT_FOUND: '用户未找到',
    USER_EXISTED: '用户 {name} 已存在'
  }
})

throw new MyError('USER_NOT_FOUND')
// output: Error: 用户未找到

throw new MyError('USER_EXISTED', { name: 'Alice' })
// output: Error: 用户 Alice 已存在

多语言

import defineError from '@sumor/error'

const MyError = defineError({
  language: 'zh', // 默认语言
  code: {
    USER_NOT_FOUND: 'User not found',
    USER_EXISTED: 'User {name} existed'
  },
  i18n: {
    en: {
      USER_NOT_FOUND: 'User not found',
      USER_EXISTED: 'User {name} existed'
    }
  }
})

const error = new MyError('USER_EXISTED', { name: 'Alice' })
error.language = 'en' // 更改错误语言
console.log(error)
// output: Error: User Alice existed

error.language = 'zh' // 更改错误语言
console.log(error)
// output: Error: 用户 Alice 已存在

将错误转换为 JSON

import defineError from '@sumor/error'

const MyError = defineError({
  code: {
    USER_NOT_FOUND: 'User not found',
    USER_EXISTED: 'User {name} existed'
  }
})

const error = new MyError('USER_EXISTED', { name: 'Alice' })
console.log(error.json())
// output: {"code":"USER_EXISTED","message":"User Alice existed"}

底层错误

import defineError from '@sumor/error'

const MyError = defineError({
  code: {
    FIELD_VERIFY_FAILED: '字段验证失败',
    FIELD_CANNOT_EMPTY: '字段 {name} 不能为空',
    FIELD_TOO_LONG: '字段 {name} 过长'
  },
  i18n: {
    en: {
      FIELD_VERIFY_FAILED: 'Field verify failed',
      FIELD_CANNOT_EMPTY: 'Field {name} cannot be empty',
      FIELD_TOO_LONG: 'Field {name} is too long'
    }
  }
})

const error = new MyError('FIELD_VERIFY_FAILED', {}, [
  new MyError('FIELD_CANNOT_EMPTY', { name: 'username' }),
  new MyError('FIELD_TOO_LONG', { name: 'password' })
])

console.log(error.json())
/* 
output: 
{
  "code":"FIELD_VERIFY_FAILED",
  "message":"Field verify failed",
  "errors":[
    {
      "code":"FIELD_CANNOT_EMPTY",
      "message":"Field username cannot be empty"
    },{
      "code":"FIELD_TOO_LONG",
      "message":"Field password is too long"
    }
  ]
}
*/

error.language = 'en'
console.log(error.json())
/*
output:
{
  "code":"FIELD_VERIFY_FAILED",
  "message":"Field verify failed",
  "errors":[
    {
      "code":"FIELD_CANNOT_EMPTY",
      "message":"Field username cannot be empty"
    },{
      "code":"FIELD_TOO_LONG",
      "message":"Field password is too long"
    }
  ]
}
*/

合并标准错误

import defineError from '@sumor/error'

const MyError = defineError({
  code: {
    FIELD_VERIFY_FAILED: 'Field verify failed',
    FIELD_CANNOT_EMPTY: 'Field {name} cannot be empty',
    FIELD_TOO_LONG: 'Field {name} is too long'
  }
})

const error = new MyError('FIELD_VERIFY_FAILED', {}, [
  new MyError('FIELD_CANNOT_EMPTY', { name: 'username' }),
  new MyError('FIELD_TOO_LONG', { name: 'password' }),
  new Error('Unknown Error')
])

console.log(error.json())
/*
output:
{
  "code":"FIELD_VERIFY_FAILED",
  "message":"Field verify failed",
  "errors":[
    {
      "code":"FIELD_CANNOT_EMPTY",
      "message":"Field username cannot be empty"
    },{
      "code":"FIELD_TOO_LONG",
      "message":"Field password is too long"
    },{
      "code":"UNKNOWN_ERROR",
      "message":"Unknown Error"
    }
  ]
}
 */