JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 22
  • Score
    100M100P100Q70221F
  • License ISC

一个用于管理错误码和错误消息映射的轻量级工具库。支持添加、获取、检查、修改和删除错误码及其对应的消息。

Package Exports

  • qn-code-msg
  • qn-code-msg/src/index.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (qn-code-msg) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

qn-code-message

一个用于管理错误码和错误消息映射的轻量级工具库。支持添加、获取、检查、修改和删除错误码及其对应的消息。

安装

npm install qn-code-message

基本用法

// ESM 导入
import { addCode, getCode, removeCode, hasCode } from "qn-code-message";

// 添加错误码
addCode(1001, "服务器错误");

// 获取错误码消息
console.log(getCode(1001)); // 输出: 服务器错误

// 检查错误码是否存在
if (hasCode(1001)) {
    console.log("错误码1001存在");
}

// 修改错误码
setCode(1001, "服务器内部错误");

// 删除错误码
removeCode(1001);

// 将错误码转为对象
const errorsObject = errorCodeObject();
console.log(errorsObject);

API 文档

暂无

模块函数

函数名 描述 参数 返回值
addCode(code, msg = '') 添加错误码及其消息 code: 错误码
msg: 错误消息
getCode(code) 获取错误码对应的消息 code: 错误码 错误消息字符串,若不存在则返回 undefined
hasCode(code) 检查错误码是否存在 code: 错误码 布尔值
setCode(code, msg = '') 设置错误码对应的消息,若不存在则添加 code: 错误码
msg: 错误消息
removeCode(code) 删除错误码 code: 错误码
getAllCode() 获取所有错误码及消息 Map 对象
errorCodeObject() 将错误码 Map 转换为普通对象 Object

预设错误码

该库包含一系列预设的错误码,导入后即可使用,具体可查看源代码中的常量定义。

测试

运行单元测试:

npm test