Socket
Socket
Sign inDemoInstall

extendable-error-class

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extendable-error-class

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


Version published
Weekly downloads
557
increased by50.95%
Maintainers
1
Weekly downloads
 
Created
Source
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

FAQs

Package last updated on 17 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc