make-error

中文文档
1.3.6发布于 2020/02/19ISCCommonJS零依赖近 30 天下载 1.9亿

Make your own error types!

原始使用文档
文档目录12 节
  1. Features
  2. Installation
  3. Node & Browserify / Webpack
  4. Browser
  5. Usage
  6. Basic named error
  7. Advanced error class
  8. Specialized error
  9. Inheritance
  10. Related
  11. Contributions
  12. License

make-error

Package Version Build Status PackagePhobia Latest Commit

Make your own error types!

Features

  • Compatible Node & browsers
  • instanceof support
  • error.name & error.stack support
  • compatible with CSP (i.e. no eval())

Installation

Node & Browserify/Webpack

Installation of the npm package:

> npm install --save make-error

Then require the package:

var makeError = require("make-error");

Browser

You can directly use the build provided at unpkg.com:

<script src="https://unpkg.com/make-error@1/dist/make-error.js"></script>

Usage

Basic named error

var CustomError = makeError("CustomError");

// Parameters are forwarded to the super class (here Error).
throw new CustomError("a message");

Advanced error class

function CustomError(customValue) {
  CustomError.super.call(this, "custom error message");

  this.customValue = customValue;
}
makeError(CustomError);

// Feel free to extend the prototype.
CustomError.prototype.myMethod = function CustomError$myMethod() {
  console.log("CustomError.myMethod (%s, %s)", this.code, this.message);
};

//-----

try {
  throw new CustomError(42);
} catch (error) {
  error.myMethod();
}

Specialized error

var SpecializedError = makeError("SpecializedError", CustomError);

throw new SpecializedError(42);

Inheritance

Best for ES2015+.

import { BaseError } from "make-error";

class CustomError extends BaseError {
  constructor() {
    super("custom error message");
  }
}

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet

版本与发布记录

make-error 共发布 22 个版本,首次发布于 2014/10/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.6latest2020/02/196 年前
1.3.52018/08/308 年前
1.3.42018/02/158 年前
1.3.32018/02/068 年前
1.3.22017/12/288 年前
1.3.12017/12/288 年前

其他发布通道:next → 0.0.0。

关于 make-error 的常见问题

make-error 是什么?

Make your own error types!

如何安装 make-error?

在项目目录运行 npm install make-error,或使用 pnpm add / yarn add / bun add。

make-error 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/make-error 是否存在。

make-error 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

make-error 可以商用吗?

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

make-error 的下载量有多少?

根据 npm 官方下载统计,make-error 在 2026/10/06 – 2026/11/04 这 30 天内被下载 193,416,472 次,日均约 6,447,216 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

make-error 还在维护吗?

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