@exodus/errors

中文文档
3.8.2发布于 2026/10/14MITTypeScriptES Module近 30 天下载 1.2万

Utilities for error handling in client code, such as sanitization

原始使用文档
文档目录9 节
  1. Usage
  2. FAQ
  3. Why using SafeError instead of built-in Errors?
  4. Why not redacting Error messages?
  5. Why not use the built-in .stack Error property?
  6. Recipes
  7. I want to preserve server errors in Safe Errors
  8. Troubleshooting
  9. A SafeError instance returns undefined stack trace?

@exodus/errors

Usage

import { SafeError } from '@exodus/errors'

try {
  throw new Error('Something went wrong')
} catch (e) {
  const safeError = SafeError.from(e)

  // It's now safe to report or log the error, even to a remote server.
  console.error({
    name: safeError.name, // Sanitized error name.
    code: safeError.code, // Optional error code (if present).
    hint: safeError.hint, // Optional error hint (if present).
    stack: safeError.stack, // Sanitized stack trace.
    timestamp: safeError.timestamp, // When the error occurred.
  })
}

FAQ

Why using SafeError instead of built-in Errors?

In large codebases, errors can be thrown from anywhere, making it impossible to audit every error message for sensitive information. A single error containing sensitive data could potentially expose user information. Centralizing error handling with SafeError makes it possible to enforce security and consistency across the board, by ensuring:

  1. Controlled Error Flow: All errors go through a single, well-tested sanitization layer before they hit error tracking systems.
  2. Enforceable Security: Error handling can be owned through codeowners and covered by tests, so nothing slips through unnoticed.

In addition to enforcing these practices, SafeError includes a few key design decisions that make it safer and more reliable than native Error objects:

  1. Message Sanitization: The message property from built-in Errors is intentionally omitted as it often contains sensitive information. Instead, a hint property is used that contains only sanitized, non-sensitive information.
  2. Native Stack Parsing: The library uses the Error.prepareStackTrace API to parse stack traces, providing consistent and reliable stack trace information across different JavaScript environments.
  3. Immutability: Once created, a SafeError instance cannot be modified, preventing tampering with error data.
  4. Safe Serialization: The toJSON method ensures safe serialization for logging or sending to error tracking services.

Why not redacting Error messages?

Parsing/sanitization of error messages is unreliable and the cost of failure is potential loss of user funds and permanent reputation damage.

Why not use the built-in .stack Error property?

Unfortunately, the built-in .stack property is mutable and outside of our control. Instead, we use the Error.prepareStackTrace API, which enables us to make sure we access the actual call stack and not a cached err.stack value that may have already been consumed and modified. We then parse it into a structured format that we can safely sanitize and control. This approach provides consistent, reliable stack traces across different environments (currently supporting both V8 and Hermes).

Recipes

[!TIP] Before diving into the recipes, you might want to get familiar with what a 'Safe String' is: https://github.com/ExodusMovement/exodus-hydra/tree/master/libraries/safe-string

I want to preserve server errors in Safe Errors

Do you control the server? If so, better send short error codes from your server instead. err.code will be passed through SafeError coercion.

If you do NOT control the server, and you know the specific error messages returned by the server, you can predefine them wrapped in safeString:

import { safeString } from '@exodus/safe-string'

// From: https://github.com/ethereum/go-ethereum/blob/master/core/txpool/errors.go.
export const KnownErrors = new Set([
  safeString`already known`,
  safeString`invalid sender`,
  safeString`transaction underpriced`,
])

You can now handle failed requests like this:

import { safeString } from '@exodus/safe-string`
import { KnownErrors } from './errors.js'

const response = await this.request(request)
const error = response?.error

if (error) {
  const message = KnownErrors.get(msg) ?? safeString`unknown error`
  throw new Error(safeString`Bad rpc response: ${message}`)
}

Troubleshooting

A SafeError instance returns undefined stack trace?

That likely means that something accesses error.stack before the Safe Error constructor had a chance to apply the custom Error.prepareStackTrace handler. This could be React, a high-level error handler, or any other framework. error.stack is computed only on the first access, so the custom handler won’t be called on subsequent attempts (see the stack.test.js for a quick demo).

If you can identify the exact place where .stack is accessed, consider capturing the stack trace explicitly like this:

import { captureStackTrace, SafeError } from '@exodus/errors'

try {
  // the devil's work
} catch (e) {
  captureStackTrace(e)
  void e.stack // Intentionally access the property to "break" it here.
  SafeError.from(e).stack // A non-empty string!
  // 🎉 Congrats — you just (hopefully) saved hours of debugging! The custom stack trace parsing logic now works because the stack was captured explicitly above.
}

版本与发布记录

@exodus/errors 共发布 25 个版本,首次发布于 2024/07/17;过去 12 个月发布了 6 个版本。

版本发布日期距今
3.8.2latest2026/10/141 个月前
3.8.12026/05/066 个月前
3.8.02026/02/248 个月前
3.7.12026/02/198 个月前
3.7.02026/02/139 个月前
3.6.02026/02/039 个月前

关于 @exodus/errors 的常见问题

@exodus/errors 是什么?

Utilities for error handling in client code, such as sanitization

如何安装 @exodus/errors?

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

@exodus/errors 支持 TypeScript 吗?

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

@exodus/errors 支持 ESM 还是 CommonJS?

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

@exodus/errors 可以商用吗?

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

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

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

@exodus/errors 还在维护吗?

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