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 = ; { Throwable; thisname = 'MyError';} MyError Throwable;
And this is how you can throw your error:
{ throw Error'Something went wrong!';}
Advantages
This library has a few advantage some other solutions might not have:
Using instanceof
wil work as expected:
try ;catcherr console; // true console; // true console; // 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.