throwable

1.0.3 • Public • Published

node-throwable

Inherit from Error without performance penalties, instanceof behaves as expected and the error stack is correct. This library can be used cross-browser using Browserify.

Usage

A lot of solutions for inheriting from Error in javascript are error-prone (pun intended) and tricky.

Using this library you can inherit Error just like you would inherit from any other javascript object/class. Here is an example using the inherits library.

var Throwable = require('throwable');
 
function MyError(wrapped)
{
        Throwable.call(this, wrapped);
        this.name = 'MyError';
}
 
require('inherits')(MyError, Throwable);

And this is how you can throw your error:

function failure()
{
        throw new MyError(Error('Something went wrong!'));
}

Advantages

This library has a few advantage some other solutions might not have:

Using instanceof wil work as expected:

try
{
        failure();
}
catch(err)
{
        console.log(err instanceof Error); // true
        console.log(err instanceof Throwable); // true
        console.log(err instanceof MyError); // true
}

And the stack, fileName (firefox), lineNumber (firefox) attributes will be set correctly (instead of pointing to a line in the Throwable constructor).

Also, the stack attribute is accessed only when you need it, Error.captureStackTrace is not used either. Accessing any of these two is a big performance hit in Node.js and Chrome.

Package Sidebar

Install

npm i throwable

Weekly Downloads

4

Version

1.0.3

License

MIT

Last publish

Collaborators

  • joris-van-der-wel