@dotcom-reliability-kit/serialize-error

中文文档
5.1.0发布于 2026/08/20MITTypeScriptES Module零依赖近 30 天下载 2万

A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON

原始使用文档
文档目录6 节
  1. Usage
  2. serializeError
  3. SerializedError type
  4. Migrating
  5. Contributing
  6. License

@dotcom-reliability-kit/serialize-error

A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON. This module is part of FT.com Reliability Kit.

Usage

Install @dotcom-reliability-kit/serialize-error as a dependency:

npm install --save @dotcom-reliability-kit/serialize-error

Include in your code:

import serializeError from '@dotcom-reliability-kit/serialize-error';

[!TIP] If you're using this package with TypeScript, we recommend using the following settings in your tsconfig.json file to avoid type errors:

{
    "esModuleInterop": true,
    "module": "nodenext",
    "moduleResolution": "nodenext"
}

serializeError

The serializeError function accepts an error-like object (e.g. an instance of Error or an object with a message property) and returns a plain JavaScript object (conforming to the SerializedError type) which contains the relevant properties:

serializeError(new Error('example message'));
// {
//     id: null,
//     fingerprint: '...',
//     name: 'Error',
//     code: 'UNKNOWN',
//     message: 'An error occurred',
//     isOperational: false,
//     relatesToSystems: [],
//     cause: null,
//     stack: '...',
//     statusCode: null,
//     data: {}
// }

You can also pass in a plain object if you already have one that looks like an error (e.g. from a JSON API response):

serializeError({
    message: 'example message'
});

SerializedError type

The SerializedError type documents the return value of the serializeError function. It will always have the following properties, extracting them from a given error object.

SerializedError.id

This is extracted from the error.id property but only if it's a String. It defaults to null.

SerializedError.fingerprint

This is a hash of the first part of the error stack, used to help group errors that occurred in the same part of the codebase. The fingerprint is null if the error does not include a stack trace.

[!WARNING]
Do not rely on the format or length of the error fingerprint as the underlying hash may change without warning. You can rely on the fingerprint being unique to the type of error being thrown.

SerializedError.name

This is extracted from the error.name property and is always cast to a String. It defaults to "Error".

SerializedError.code

This is extracted from the error.code property and is always cast to a String. It defaults to "UNKNOWN".

SerializedError.message

This is extracted from the error.message property and is always cast to a String. It defaults to "An error occurred", which is not very helpful but we want this library to be resilient to unexpected input data.

SerializedError.isOperational

This indicates whether the error is operational (known about). See the documentation for OperationalError for more information. It is extracted from the error.isOperational property. It is always cast to a Boolean and defaults to false.

SerializedError.relatesToSystems

This array contains a list of system codes which are related to the error. It defaults to an empty array.

SerializedError.cause

This is an error instance extracted from the error.cause property, which is serialized before being assigned. It defaults to null.

SerializedError.errors

This is an array of error instances extracted from the error.errors property (e.g. from an AggregateError). The errors are serialized before being assigned.

SerializedError.stack

This is extracted from the error.stack property. If this property is not a string, then it will default to null.

SerializedError.statusCode

This is extracted from the error.statusCode property first, then the error.status property otherwise (we use look at both properties to maintain compatibility with third-party request and error libraries). These are always cast to a Number and this property defaults to null.

SerializedError.data

This is extracted from the error.data property. If this property is not a plain object, then it will default to an empty object: {}.

Migrating

Consult the Migration Guide if you're trying to migrate to a later major version of this package.

Contributing

See the central contributing guide for Reliability Kit.

License

Licensed under the MIT license.
Copyright © 2022, The Financial Times Ltd.

版本与发布记录

@dotcom-reliability-kit/serialize-error 共发布 27 个版本,首次发布于 2022/05/16;过去 12 个月发布了 6 个版本。

版本发布日期距今
5.1.0latest2026/08/202 个月前
5.0.12026/07/144 个月前
5.0.02026/06/234 个月前
5.0.0-beta.2prerelease2026/06/035 个月前
5.0.0-beta.12026/03/177 个月前
4.2.02025/12/0411 个月前

关于 @dotcom-reliability-kit/serialize-error 的常见问题

@dotcom-reliability-kit/serialize-error 是什么?

A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON

如何安装 @dotcom-reliability-kit/serialize-error?

在项目目录运行 npm install @dotcom-reliability-kit/serialize-error,或使用 pnpm add / yarn add / bun add。

@dotcom-reliability-kit/serialize-error 支持 TypeScript 吗?

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

@dotcom-reliability-kit/serialize-error 支持 ESM 还是 CommonJS?

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

@dotcom-reliability-kit/serialize-error 需要什么版本的 Node.js?

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

@dotcom-reliability-kit/serialize-error 可以商用吗?

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

@dotcom-reliability-kit/serialize-error 的下载量有多少?

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

@dotcom-reliability-kit/serialize-error 还在维护吗?

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