@ deessejs/ errors (英语)
轻量级,类型安全的错误处理,用于TypeScript——Python风格. 基于函数的API,例外连锁,分级继承,和丰富的出错语义. 仅限无害环境管理,为一级互操作性设计 @deessejs/fp'结果并尝试.
兄弟姐妹项目:
@deessejs/fp提供Result财务报告和审定财务报表Try本地融合类型@deessejs/errors错误工厂。 将它们安装在一起,以获得一个没有胶合码的完整错误处理故事.
包含的内容
| 层 | 你得到的东西 | 为什么这很重要 |
|---|---|---|
error() | 定义带有名称、消息模板、字段和继承的错误工厂. | Python 风格的错误定义不包含类. |
.from() 连锁 | 由于链条通过 .from() + 键 causes() 转过身来. | 将错误连接在一起,同时保留整个链条上下文. |
| 单一或多重继承 | inherits: 接受工厂或阵列. | 组织匹配您域的错误等级 . |
is() 类型检查 | 具有遗产支持的运行时间和类型安全分类器. | 毫不含糊地歧视错误 instanceof。 。 。 . |
.addNote() | 在错误实例中附加运行时间上下文 . | Python 3.11风格的笔记 (PEP 678),用于Brack-freadcrumbs调试. |
| 信件模板 | {field} 占位符为 :upper, (中文(简体) ), :lower, (中文(简体) ), :json 修饰相. | 施工时结构化字段的可读信息. |
| 标准Schema 字段 | 接受Zod / Valibot / ArkType 计划. | 验证每个错误实例的结构化数据,没有手卷式的警卫. |
raise() | 平庸地投出帮助者. | 收缩型( N)never(中文(简体) ) raise(err) 用于控制流可读性. |
@deessejs/fp 融合 | Result编号 :Try 接受 ErrorInstance 直接说. | 类型安全出错管线由端到端,不设弦-error脚枪. |
安装
npm install @deessejs/errors
@deessejs/fp 是可选的 - 如果您想要作曲, 请安装它 Result编号 :Try 类型 ErrorInstance。 。 。 .
使用量
import { z } from 'zod';
import { error, raise, is, causes } from '@deessejs/errors';
// Define a factory with a Standard Schema. The `message` is a
// function that receives the schema's *output* (post-transform).
// The factory's call signature requires the schema's *input*,
// so callers can supply the validated shape directly.
const ValidationError = error({
name: 'ValidationError',
fields: z.object({
field: z.string(),
reason: z.string(),
}),
message: (data) => `Field "${data.field}" is invalid: ${data.reason}`,
});
// Construct a typed error. The schema validates the input.
const err = ValidationError({ field: 'email', reason: 'invalid format' });
// err.message === 'Field "email" is invalid: invalid format'
// Chain a cause. .from() replaces the previous cause; the
// previous cause is reachable through its own .cause link.
const cause = error({ name: 'NetworkError' })();
err.from(cause);
// Throw it
raise(err);
// Later, type-check and walk the chain
is(err, ValidationError); // true
causes(err); // [cause]
为什么这个图书馆
- 默认很简单. 没有阶级等级,没有装饰者在反省之中.
- 仅限无害环境管理. 现代包装,没有CJS shim,没有
module编号 :main重叠. - 最小运行时间. 唯一的运行时间依赖是
@standard-schema/spec。 。 。 . - 类型脚本一等. 严格类型, 不
any出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出入出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出入出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出 JSDoc在每个公共符号上. - 真正的测试. 具有类型级和运行时间测试的活性测试,包括因子链转录.
引擎兼容性
| 运行时间 | 需求 |
|---|---|
| 节点.js | >=22.14.0 |
| 小时 | 10 促进发展 |
| 类型脚本 | 5.4 或以后(公众) error() 超载使用 NoInfer<T>(中文(简体) ) |
仅限无害环境管理。 使用 CJS 解析器的消费者需要使用动态 import() 或迁移到无害环境管理.
可用命令
| 命令 | 它会做什么 |
|---|---|
pnpm build | 构建 dist/ (单位:千美元)tsc -p tsconfig.build.json(中文(简体) ) |
pnpm test | 以监视模式运行 vitest |
pnpm test:run | 运行一次vitest |
pnpm type-check | tsc --noEmit |
pnpm lint | 运行 ESLint |
捐款
打开一个问题来讨论更大的变化. 对于打字,断开的链接和小修正,公关是受欢迎的.
在提交公关前:
- 运行
pnpm test:run财务报告和审定财务报表pnpm lint。 。 。 . - 添加一个
.changeset/<topic>.md如果更改是用户化的(patch / small / major) . - 更新
docs/internal/product/README.md如果 API 表面改变.
许可证
麻省理工学院。详情见LICENSE文件.
鸣谢
README 版式基于 解剖/包装-模版 并借用其结构 中学/小学,适合 @deessejs/errors API表面.