# @chubbyts/chubbyts-throwable-to-error 中文文档

规范地址：https://npmcn.com/package/%40chubbyts/chubbyts-throwable-to-error/

版本：2\.4\.1
npm 元数据更新时间：2026-11-08T19:39:35.255Z
中文译文更新时间：2026-11-09T17:15:21.327Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40chubbyts/chubbyts-throwable-to-error
原文对照：https://npmcn.com/package/%40chubbyts/chubbyts-throwable-to-error/?lang=original
源码仓库：https://github.com/chubbyts/chubbyts-throwable-to-error

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：10 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=22
- 运行依赖：0 个
- 首次发布：2022-06-09T19:04:53.742Z
- 最新版本发布：2026-11-08T19:39:35.052Z
- 近 30 天下载量：2159（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：23（近 12 个月 11 个）

## 中文简介

将任何抛出值转换为错误.

## 安装

```sh
npm install @chubbyts/chubbyts-throwable-to-error
```

也可以使用：`pnpm add @chubbyts/chubbyts-throwable-to-error`、`yarn add @chubbyts/chubbyts-throwable-to-error`、`bun add @chubbyts/chubbyts-throwable-to-error`

## README

# 胖子可扔到过头

[![CI](https://github.com/chubbyts/chubbyts-throwable-to-error/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/chubbyts/chubbyts-throwable-to-error/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/chubbyts/chubbyts-throwable-to-error/badge.svg?branch=master)](https://coveralls.io/github/chubbyts/chubbyts-throwable-to-error?branch=master) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchubbyts%2Fchubbyts-throwable-to-error%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/chubbyts/chubbyts-throwable-to-error/master) [![npm-version](https://img.shields.io/npm/v/@chubbyts/chubbyts-throwable-to-error.svg)](https://www.npmjs.com/package/@chubbyts/chubbyts-throwable-to-error)

[![bugs](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=bugs)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![code\_smells](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=code_smells)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![coverage](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=coverage)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![duplicated\_lines\_density](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![ncloc](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=ncloc)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![sqale\_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![alert\_status](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=alert_status)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![reliability\_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![security\_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=security_rating)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![sqale\_index](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error) [![vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=chubbyts_chubbyts-throwable-to-error&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=chubbyts_chubbyts-throwable-to-error)

## 说明

将任何抛出值转换为 `Error`。 。 。 .

在 Java 脚本中, `throw` 接受任何东西:字符串、数字、普通对象, `null`, (中文(简体) ), `undefined`,所以类型脚本 类型 a `catch` 变量为 `unknown`。 。 。 . `throwableToError` 使这个值正常化为真实的 `Error`所以你们剩下的人 代码(博客、错误反应、, `cause` 链条)可以依赖 `message` 财务报告和审定财务报表 `name` 在场.

### 为什么用它

-   **类型安全**: 转弯 `unknown` 输入 `Error` 没有不安全 `as Error` 铸作.
-   **从不扔**: 循环参考,投掷 `toJSON` 编号 : `toString` / 获取器和不可序列值被处理 报告为 `[unserializable]` 而不是崩溃您的错误处理器.
-   **锁定输出**: 超过 2048 个字符的信件被切换(后缀) `...`),所以一个巨大的抛出物体不能 冲出你的日志.
-   **使真实错误不受影响**页:1 `Error` (或子类)与原来的堆栈一样被还原.
-   **小声**:一个单一的函数,没有依赖关系.

## 所需资源

-   节点:

## 安装

穿过 [国家预防机制](https://www.npmjs.com/) 作为 [@ chubbyts/ chubbyts-可推-tror](https://www.npmjs.com/package/@chubbyts/chubbyts-throwable-to-error)。 。 。 .

```sh
npm i @chubbyts/chubbyts-throwable-to-error@^2.4.1
```

## 使用量

```ts
import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/throwable-to-error';

try {
  throw { key: 'value' };
} catch (e) {
  const error = throwableToError(e);

  error.name; // 'object'
  error.message; // '{"key":"value"}'
}
```

## 行为

| 丢入值 | `name` | `message` |
| --- | --- | --- |
| `new Error('example')` | 不变( 相同实例) | 不变( 相同实例) |
| `'example'` | `'string'` | `'example'` |
| `42` | `'number'` | `'42'` |
| `true` | `'boolean'` | `'true'` |
| `undefined` | `'undefined'` | `'undefined'` |
| `null` | `'null'` | `'null'` |
| `Symbol('example')` | `'symbol'` | `'Symbol(example)'` |
| `() => null` | `'function'` | `'() => null'` |
| `{ key: 'value' }` | `'object'` | `'{"key":"value"}'` |
| `['example']` | `'object'` | `'["example"]'` |
| 圆形参考对象 | `'object'` | `'[unserializable]'` |

-   对象(和数组)以 `JSON.stringify`,其他所有值为 `String`。 。 。 .
-   这个 `name` 这是 `typeof` 丢出值 (`'null'` 页:1 `null`) (中文(简体) ).
-   这个 `stack` 转换错误为 `undefined`:它只会指出 `throwableToError` 自己,不是去那个地方 投出数值的地方.

## 版权

2026 道米尼克·佐格(英语:

## 常见问题

### @chubbyts/chubbyts-throwable-to-error 是什么？

将任何抛出值转换为错误\.

### 如何安装 @chubbyts/chubbyts-throwable-to-error？

在项目目录运行 npm install @chubbyts/chubbyts-throwable-to-error，或使用 pnpm add / yarn add / bun add。

### @chubbyts/chubbyts-throwable-to-error 支持 TypeScript 吗？

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

### @chubbyts/chubbyts-throwable-to-error 支持 ESM 还是 CommonJS？

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

### @chubbyts/chubbyts-throwable-to-error 需要什么版本的 Node\.js？

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

### @chubbyts/chubbyts-throwable-to-error 可以商用吗？

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

### @chubbyts/chubbyts-throwable-to-error 的下载量有多少？

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

### @chubbyts/chubbyts-throwable-to-error 还在维护吗？

最新版本 2\.4\.1 发布于 2026/11/09（1 天前）；至今共发布 23 个版本，其中过去 12 个月发布了 11 个；首次发布于 2022/06/10。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

