# extendable-error-class

> Extend the Error Class with ES2015/ES5/Babel, i.e. fix for `class MyError extends Error{}`

Latest version **0.1.2** (published 2019-04-17) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install extendable-error-class
pnpm add extendable-error-class
yarn add extendable-error-class
bun add extendable-error-class
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2019-04-17 |
| First published | 2016-07-28 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Maintainers | brillout |

## Links

- npm: https://www.npmjs.com/package/extendable-error-class
- Repository: https://github.com/brillout/extendable-error-class
- Homepage: https://github.com/brillout/extendable-error-class#readme
- Issues: https://github.com/brillout/extendable-error-class/issues
- npm.io page: https://npm.io/package/extendable-error-class

## Recent versions

- 0.1.2 (latest) — 2019-04-17
- 0.1.1 — 2016-07-28
- 0.1.0 — 2016-07-28

## README

#### Usage

```js
const ExtendableError = require('extendable-error-class');

class MyError extends ExtendableError {
    constructor(m) {
        super(m);
        
        // Optional - ExtendableError sets `name` to your class's name itself,
        // but minification, etc. can change that, so you may want to do it
        // explicitly.
        this.name = 'MyError';
    }
}

```
`extendable-error-class` has no dependencies.


### Why

Because the following doesn't work as expected.
```js
class MyError extends Error {
    constructor(m) {
        super(m);
    }
}
```



### How

The workaround is based on `babel-plugin-transform-builtin-extend` and following code

```js
const ExtendableError = class extends Error {
    constructor(message) {
        super(message);
        this.name = this.constructor.name;
        this.message = message;
        if (typeof Error.captureStackTrace === 'function') {
            Error.captureStackTrace(this, this.constructor);
        } else {
            this.stack = (new Error(message)).stack;
        }
    }
};
```

The code is already compiled with `babel` and `babel-plugin-transform-builtin-extend`. Therefore one is able to use this workaround without adding any dependency.

See http://stackoverflow.com/questions/31089801/extending-error-in-javascript-with-es6-syntax

---
_Source: https://npm.io/package/extendable-error-class · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
