@dotcom-reliability-kit/crash-handler

中文文档
6.0.3发布于 2026/11/11MITTypeScriptES Module近 30 天下载 2959

A method to bind an uncaught exception handler to ensure that fatal application errors are logged

原始使用文档
文档目录8 节
  1. Usage
  2. registerCrashHandler
  3. Configuration options
  4. Compatibility
  5. Migrating from Sentry
  6. Migrating
  7. Contributing
  8. License

@dotcom-reliability-kit/crash-handler

A method to bind an uncaught exception handler to ensure that fatal application errors are logged. It is a replacement for Sentry fatal error logging. This module is part of FT.com Reliability Kit.

Usage

Install @dotcom-reliability-kit/crash-handler as a dependency:

npm install --save @dotcom-reliability-kit/crash-handler

Include in your code:

import registerCrashHandler from '@dotcom-reliability-kit/crash-handler';

[!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"
}

registerCrashHandler

The registerCrashHandler function can be used to bind an event handler to the Node.js process.uncaughtException event. This ensures that your application logs a final message before crashing in the event on an unexpected error or promise rejection.

This function should only ever be called once in your app, normally alongside all your setup code (e.g. alongside creating an Express app).

[!TIP]
It's not a requirement, but generally the earlier the better with registering an uncaught exception handler – the sooner you register it the more likely you are to catch uncaught exceptions.

registerCrashHandler();

If an error is thrown which will crash your application, error information will be logged and then the process will exit with the value of process.exitCode or 1.

Configuration options

Config options can be passed into the registerCrashHandler function as an object with any of the keys below.

registerCrashHandler({
    // Config options go here
});

options.logger

A logger object which implements two methods, error and warn, which have the following permissive signature:

type LogMethod = (...logData: any) => any;

This is passed directly onto the relevant log-error method, see the documentation for that package for more details.

options.process

The Node.js Process object to bind the error handling event to. You may use this if you are using a child process or want to mock the process object in your tests.

registerCrashHandler({
    process: myProcessObject
});

Compatibility

Migrating from Sentry

The Reliability Kit crash handler is a replacement for Sentry's uncaught exception handling, which your app is likely to be using. You'll need to migrate away from Sentry in order to use this module. We maintain a migration guide for this on Confluence.

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/crash-handler 共发布 63 个版本,首次发布于 2022/10/21;过去 12 个月发布了 8 个版本。

版本发布日期距今
6.0.3latest2026/11/112 天前
6.0.22026/08/202 个月前
6.0.12026/07/144 个月前
6.0.02026/06/234 个月前
6.0.0-beta.1prerelease2026/06/035 个月前
5.1.82026/03/128 个月前

关于 @dotcom-reliability-kit/crash-handler 的常见问题

@dotcom-reliability-kit/crash-handler 是什么?

A method to bind an uncaught exception handler to ensure that fatal application errors are logged

如何安装 @dotcom-reliability-kit/crash-handler?

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

@dotcom-reliability-kit/crash-handler 支持 TypeScript 吗?

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

@dotcom-reliability-kit/crash-handler 支持 ESM 还是 CommonJS?

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

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

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

@dotcom-reliability-kit/crash-handler 可以商用吗?

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

@dotcom-reliability-kit/crash-handler 的下载量有多少?

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

@dotcom-reliability-kit/crash-handler 还在维护吗?

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