@deessejs/errors

中文文档
2.0.0发布于 2026/10/09MITTypeScriptES Module构建来源已验证近 30 天下载 2785

Lightweight, type-safe error handling for TypeScript — Python-style exception chaining, hierarchical inheritance, and rich error semantics. Function-based API, ESM-only, designed for first-class interoperability with @deessejs/fp.

原始使用文档
文档目录9 节
  1. What is included
  2. Install
  3. Usage
  4. Why this library
  5. Engine compatibility
  6. Available commands
  7. Contributing
  8. License
  9. Acknowledgements

@deessejs/errors

npm TypeScript CI License: MIT

Lightweight, type-safe error handling for TypeScript — Python-style. Function-based API, exception chaining, hierarchical inheritance, and rich error semantics. ESM-only, designed for first-class interoperability with @deessejs/fp's Result and Try.

Sibling projects: @deessejs/fp provides Result and Try types that integrate natively with @deessejs/errors error factories. Install them together to get a complete error-handling story without glue code.


What is included

LayerWhat you getWhy it matters
error()Define error factories with name, message templates, fields, and inheritance.Python-style error definitions without classes.
.from() chainingCause chain via .from() + causes() traversal.Link errors together while preserving the full chain context.
Single or multiple inheritanceinherits: accepts a factory or an array.Organize error hierarchies that match your domain.
is() type checkingRuntime and type-safe classifier with inheritance support.Discriminate errors without brittle instanceof.
.addNote()Attach runtime context to error instances.Python 3.11-style notes (PEP 678) for trail-of-breadcrumbs debugging.
Message templates{field} placeholders with :upper, :lower, :json modifiers.Readable messages composed from structured fields at construction time.
Standard Schema fieldsAccepts Zod / Valibot / ArkType schemas.Validated structured data on every error instance, no hand-rolled guards.
raise()Idiomatic throw helper.Type-narrowed (never) raise(err) for control-flow readability.
@deessejs/fp integrationResult/Try accept ErrorInstance directly.Type-safe error pipelines end-to-end, no string-error footguns.

Install

npm install @deessejs/errors

@deessejs/fp is optional - install it if you want to compose Result/Try types around ErrorInstance.

Usage

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]

Why this library

  • Simple by default. No class hierarchies, no decorators under reflection.
  • ESM-only. Modern packaging, no CJS shim, no module/main duplication.
  • Minimal runtime. The only runtime dependency is @standard-schema/spec.
  • TypeScript first-class. Strict types, no any leakages, full inference. JSDoc on every public symbol.
  • Real testing. Vitest with type-level and runtime tests, including cause-chain traversal.

Engine compatibility

RuntimeRequired
Node.js>=22.14.0
pnpm10 for development
TypeScript5.4 or later (the public error() overloads use NoInfer<T>)

ESM-only. Consumers using a CJS resolver need to use dynamic import() or migrate to ESM.

Available commands

CommandWhat it does
pnpm buildBuild dist/ (tsc -p tsconfig.build.json)
pnpm testRun vitest in watch mode
pnpm test:runRun vitest once
pnpm type-checktsc --noEmit
pnpm lintRun ESLint

Contributing

Open an issue to discuss larger changes. For typos, broken links, and small fixes, PRs are welcome.

Before submitting a PR:

  1. Run pnpm test:run and pnpm lint.
  2. Add a .changeset/<topic>.md if the change is user-facing (patch / minor / major).
  3. Update docs/internal/product/README.md if the API surface changes.

License

MIT. See the LICENSE file for details.

Acknowledgements

The README layout is based on the deessejs/package-template and borrows its structure from the deessejs/fp README, adapted for the @deessejs/errors API surface.

版本与发布记录

@deessejs/errors 共发布 8 个版本,首次发布于 2026/06/01;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/091 个月前
1.4.02026/08/053 个月前
1.3.32026/08/053 个月前
1.3.22026/08/043 个月前
1.2.22026/08/033 个月前
1.1.12026/06/055 个月前

关于 @deessejs/errors 的常见问题

@deessejs/errors 是什么?

Lightweight, type-safe error handling for TypeScript — Python-style exception chaining, hierarchical inheritance, and rich error semantics. Function-based API, ESM-only, designed for first-class interoperability with @deessejs/fp.

如何安装 @deessejs/errors?

在项目目录运行 npm install @deessejs/errors,或使用 pnpm add / yarn add / bun add。

@deessejs/errors 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deessejs/errors 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@deessejs/errors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.14.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@deessejs/errors 可以商用吗?

@deessejs/errors 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deessejs/errors 的下载量有多少?

根据 npm 官方下载统计,@deessejs/errors 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,785 次,日均约 93 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@deessejs/errors 还在维护吗?

最新版本 2.0.0 发布于 2026/10/09(1 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/06/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。