1 | # Make Error Cause
|
2 |
|
3 | [![NPM version][npm-image]][npm-url]
|
4 | [![NPM downloads][downloads-image]][downloads-url]
|
5 | [![Build status][travis-image]][travis-url]
|
6 | [![Test coverage][coveralls-image]][coveralls-url]
|
7 |
|
8 | > Make your own nested errors.
|
9 |
|
10 | ## Features
|
11 |
|
12 | - Compatible with node.js and browsers
|
13 | - Works with `instanceof`
|
14 | - Automatic full stack traces in node.js (supports [`inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options))
|
15 | - Output full stack trace with `fullStack(err)`
|
16 | - Extends [`make-error`](https://github.com/JsCommunity/make-error)
|
17 |
|
18 | ## Installation
|
19 |
|
20 | ```
|
21 | npm install make-error-cause --save
|
22 | ```
|
23 |
|
24 | ## Usage
|
25 |
|
26 | ```js
|
27 | import { BaseError, fullStack } from "make-error-cause";
|
28 |
|
29 | class CustomError extends BaseError {
|
30 | constructor(message, cause) {
|
31 | super(message, cause);
|
32 | }
|
33 | }
|
34 |
|
35 | const error = new Error("Boom!");
|
36 | const customError = new CustomError("Another boom!", error);
|
37 |
|
38 | console.log(customError); // Automatically prints full stack trace using `fullStack(this)`.
|
39 | console.log(customError.cause); // Check causes via the `.cause` property.
|
40 |
|
41 | console.log(customError instanceof Error); //=> true
|
42 | ```
|
43 |
|
44 | ## Attribution
|
45 |
|
46 | Inspired by [`verror`](https://www.npmjs.com/package/verror), and others, but created lighter and without core dependencies for browser usage.
|
47 |
|
48 | Other references:
|
49 |
|
50 | - [Java](https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html)
|
51 | - [Python](https://www.python.org/dev/peps/pep-3134/)
|
52 |
|
53 | ## License
|
54 |
|
55 | Apache 2.0
|
56 |
|
57 | [npm-image]: https://img.shields.io/npm/v/make-error-cause.svg?style=flat
|
58 | [npm-url]: https://npmjs.org/package/make-error-cause
|
59 | [downloads-image]: https://img.shields.io/npm/dm/make-error-cause.svg?style=flat
|
60 | [downloads-url]: https://npmjs.org/package/make-error-cause
|
61 | [travis-image]: https://img.shields.io/travis/blakeembrey/make-error-cause.svg?style=flat
|
62 | [travis-url]: https://travis-ci.org/blakeembrey/make-error-cause
|
63 | [coveralls-image]: https://img.shields.io/coveralls/blakeembrey/make-error-cause.svg?style=flat
|
64 | [coveralls-url]: https://coveralls.io/r/blakeembrey/make-error-cause?branch=master
|