@enzoferey/ethers-error-parser

中文文档
0.2.3发布于 2022/12/16MITTypeScriptESM + CJS零依赖近 30 天下载 5923

Tests npm version [![codecov](https://

原始使用文档
文档目录6 节
  1. Highlights
  2. Why
  3. Work in progress
  4. Getting started
  5. Return value
  6. TypeScript usage

ethers-error-parser

Tests npm version codecov npm bundle size

Parse Ethers.js errors with ease 💅🏻

Highlights

  • Zero dependencies 🧹
  • Lightweight 📦
  • Simple to use ⚡️
  • Work in progress 🚧

Why

Ethers.js is well known for its cryptic error messages. Whenever a transaction fails you will get an error message that combines plain text and JSON stringified string like this:

Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32603,"message":"execution reverted: Code has already claimed","data":{"originalError":{"code":3,"data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000018436f64652068617320616c726561647920636c61696d65640000000000000000","message":"execution reverted: Code has already claimed"}}}, method="estimateGas", transaction={"from":"0xC16f5C62b29704F7aBECb27A3cb7E12a91383261","to":"0xb21FFFd62BD2f4aBd2a1dC34A2302Fda364977a0","data":"0xd2c34d3f0000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000063132333435360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004153801a64156372ec7cd1d91868dd35ed68972dfa8b347c59db14bc49b753ed576fbe8a2bc00b6d0ba5dc8429c01748ae55e87dffa9547aafeb844aa40bb6c3e31b00000000000000000000000000000000000000000000000000000000000000","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.5.1)

Your users deserve to get amazing feedback on failing transactions. Thanks to ethers-error-parser you can do that with ease.

Work in progress

This package is a work in progress. Although it is not yet complete and many Ethers errors could be handled more elegantly, it is safe to use in production as it is. Some insights is better than no insights.

This package is being used in different production projects and it is in constant evolution based on the needs of these projects. If you find some error that is not handled yet or that does not provide a great context, please open an issue or pull request 🙏

Getting started

  1. Install the package
yarn add @enzoferey/ethers-error-parser
  1. Use it
import { getParsedEthersError } from "@enzoferey/ethers-error-parser";

try {
  const transaction = await someContract.someMethod();
  await transaction.wait();
} catch (error) {
  const parsedEthersError = getParsedEthersError(error);
  // parsedError.errorCode - contains a well defined error code (see full list below)
  // parsedError.context - contains a context based string providing additional information
  // profit ! 💅🏻
}

Return value

When using getParsedEthersError you will get back an object containing a well known errorCode property and an optional context property with additional information. The TypeScript type definition looks like the following:

interface ReturnValue {
  errorCode: string;
  context?: string;
}

Here is the complete list of returned objects:

errorCodecontext
TRANSACTION_RAN_OUT_OF_GASThe transaction gas limit as a string.
TRANSACTION_UNDERPRICEDundefined
REJECTED_TRANSACTIONThe reason why the transaction rejected.
CALL_REVERTEDThe reason why the call reverted.
EXECUTION_REVERTEDThe reason why the transaction reverted.
NONCE_TOO_LOWThe transaction nonce as a string.
INSUFFICIENT_FUNDS_FOR_GASundefined
MAX_PRIORITY_FEE_PER_GAS_HIGHER_THAN_MAX_FEE_PER_GASundefined
MAX_FEE_PER_GAS_LESS_THAN_BLOCK_BASE_FEEundefined
UNKNOWN_ERRORSome code or description of the error if available. undefined otherwise.

The error codes strings can be accesses via the RETURN_VALUE_ERROR_CODES constant that the package exports.

If you find some error that is not handled yet or that does not provide a great context, please open an issue or pull request 🙏

TypeScript usage

If you are using TypeScript, you may find the required types exported at the root of the package. For example, when handling a common try / catch statement, you will need to cast the error to use the internal EthersError type:

import {
  getParsedEthersError,
  EthersError,
} from "@enzoferey/ethers-error-parser";

try {
  const transaction = await someContract.someMethod();
  await transaction.wait();
} catch (error) {
  const parsedEthersError = getParsedEthersError(error as EthersError);
}

版本与发布记录

@enzoferey/ethers-error-parser 共发布 6 个版本,首次发布于 2022/07/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.3latest2022/12/163 年前
0.2.22022/09/254 年前
0.2.12022/09/184 年前
0.2.02022/08/214 年前
0.1.12022/07/254 年前
0.1.02022/07/244 年前

关于 @enzoferey/ethers-error-parser 的常见问题

@enzoferey/ethers-error-parser 是什么?

[![codecov](https://

如何安装 @enzoferey/ethers-error-parser?

在项目目录运行 npm install @enzoferey/ethers-error-parser,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ethers(^5.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@enzoferey/ethers-error-parser 支持 TypeScript 吗?

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

@enzoferey/ethers-error-parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@enzoferey/ethers-error-parser 可以商用吗?

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

@enzoferey/ethers-error-parser 的下载量有多少?

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

@enzoferey/ethers-error-parser 还在维护吗?

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