error-ex

中文文档
1.3.4发布于 2025/09/15MIT近 30 天下载 4亿

Easy error subclassing and stack customization

原始使用文档
文档目录3 节
  1. Examples
  2. API
  3. License

node-error-ex Travis-CI.org Build Status Coveralls.io Coverage Rating

Easily subclass and customize new Error types

Examples

To include in your project:

var errorEx = require('error-ex');

To create an error message type with a specific name (note, that ErrorFn.name will not reflect this):

var JSONError = errorEx('JSONError');

var err = new JSONError('error');
err.name; //-> JSONError
throw err; //-> JSONError: error

To add a stack line:

var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')});

var err = new JSONError('error')
err.fileName = '/a/b/c/foo.json';
throw err; //-> (line 2)-> in /a/b/c/foo.json

To append to the error message:

var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')});

var err = new JSONError('error');
err.fileName = '/a/b/c/foo.json';
throw err; //-> JSONError: error in /a/b/c/foo.json

API

errorEx([name], [properties])

Creates a new ErrorEx error type

  • name: the name of the new type (appears in the error message upon throw; defaults to Error.name)
  • properties: if supplied, used as a key/value dictionary of properties to use when building up the stack message. Keys are property names that are looked up on the error message, and then passed to function values.
    • line: if specified and is a function, return value is added as a stack entry (error-ex will indent for you). Passed the property value given the key.
    • stack: if specified and is a function, passed the value of the property using the key, and the raw stack lines as a second argument. Takes no return value (but the stack can be modified directly).
    • message: if specified and is a function, return value is used as new .message value upon get. Passed the property value of the property named by key, and the existing message is passed as the second argument as an array of lines (suitable for multi-line messages).

Returns a constructor (Function) that can be used just like the regular Error constructor.

var errorEx = require('error-ex');

var BasicError = errorEx();

var NamedError = errorEx('NamedError');

// --

var AdvancedError = errorEx('AdvancedError', {
    foo: {
        line: function (value, stack) {
            if (value) {
                return 'bar ' + value;
            }
            return null;
        }
    }
})

var err = new AdvancedError('hello, world');
err.foo = 'baz';
throw err;

/*
    AdvancedError: hello, world
        bar baz
        at tryReadme() (readme.js:20:1)
*/

errorEx.line(str)

Creates a stack line using a delimiter

This is a helper function. It is to be used in lieu of writing a value object for properties values.

  • str: The string to create
    • Use the delimiter %s to specify where in the string the value should go
var errorEx = require('error-ex');

var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')});

var err = new FileError('problem reading file');
err.fileName = '/a/b/c/d/foo.js';
throw err;

/*
    FileError: problem reading file
        in /a/b/c/d/foo.js
        at tryReadme() (readme.js:7:1)
*/

errorEx.append(str)

Appends to the error.message string

This is a helper function. It is to be used in lieu of writing a value object for properties values.

  • str: The string to append
    • Use the delimiter %s to specify where in the string the value should go
var errorEx = require('error-ex');

var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')});

var err = new SyntaxError('improper indentation');
err.fileName = '/a/b/c/d/foo.js';
throw err;

/*
    SyntaxError: improper indentation in /a/b/c/d/foo.js
        at tryReadme() (readme.js:7:1)
*/

License

Licensed under the MIT License. You can find a copy of it in LICENSE.

版本与发布记录

error-ex 共发布 18 个版本,首次发布于 2015/08/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.4latest2025/09/151.1 年前
1.3.32025/09/081.2 年前
1.3.22018/06/198 年前
1.3.12017/03/049 年前
1.3.02015/11/0411 年前
1.2.02015/08/2711 年前

关于 error-ex 的常见问题

error-ex 是什么?

Easy error subclassing and stack customization

如何安装 error-ex?

在项目目录运行 npm install error-ex,或使用 pnpm add / yarn add / bun add。

error-ex 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/error-ex 是否存在。

error-ex 可以商用吗?

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

error-ex 的下载量有多少?

根据 npm 官方下载统计,error-ex 在 2026/10/06 – 2026/11/04 这 30 天内被下载 400,331,382 次,日均约 13,344,379 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

error-ex 还在维护吗?

最新版本 1.3.4 发布于 2025/09/15(1.1 年前);至今共发布 18 个版本,其中过去 12 个月发布了 0 个;首次发布于 2015/08/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。