extendable-error-class

0.1.2 • Public • Published

Usage

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.

class MyError extends Error {
    constructor(m) {
        super(m);
    }
}

How

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

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

Readme

Keywords

none

Package Sidebar

Install

npm i extendable-error-class

Weekly Downloads

1,306

Version

0.1.2

License

CC0-1.0

Unpacked Size

11.4 kB

Total Files

5

Last publish

Collaborators

  • brillout