Extensible Custom Error
JavaScript extensible custom error that can take a message and/or an Error object
{} 'message'; // Take a messageerror; // Take an error'message' error; // Take a message and an error
Contents
Features
There are some pains around JavaScript error handling. Two of them are:
- Define custom errors easily
- Wrap errors without losing any data
This ExtensibleCustomError
class enables you to do both - you can define your custom errors easily and wrap errors with them while merging stack traces prettily.
Define custom errors easily
To define custom errors in Vanilla JS, you need to set names and stack traces manually, but you no longer need to do that with ExtensibleCustomError
.
{}
Wrap errors without losing any data
Built-in errors only take a message, so they can't wrap any errors, which means stack traces so far will be lost.
However, ExtensibleCustomError
can take a message and/or an Error object while merging stack traces.
catch error throw error;
catch error throw 'message' error;
Installation
Using npm:
$ npm install extensible-custom-error
Using Yarn:
$ yarn add extensible-custom-error
Usage
Define custom errors
const ExtensibleCustomError = ; // That's it! {}
// Should you need to set custom properties { // Ensure calling the super constructor super...args; Object; }
N.B. With an uglifier, class names might get obsecure. See this issue comment.
Instantiate custom errors
You can instantiate your custom errors in the same way as built-in errors.
// Throw it as usual!throw 'Steel is my Body and Fire is my Blood';
try // Do something that may cause errors catch error // Pass an error instance, then stack traces will be merged throw error;
try // Do something that may cause errors catch error // Pass a message and an error instance, then stack traces will be merged throw 'I have created over a Thousand Blades' error;
Examples
Wrap an error
If you run:
const ExtensibleCustomError = ; {} { throw 'Unknown to Death, Nor known to Life';} { try ; catch error throw error; } { try ; catch error console; } ;
you'll get:
MyError: Error: Unknown to Death, Nor known to Life at wrapErrorWithMyError Error: Unknown to Death, Nor known to Life at throwBuiltinError at wrapErrorWithMyError at main at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain
Wrap an error while passing a new message
If you run:
const ExtensibleCustomError = ; {} { throw 'Have withstood Pain to create many Weapons';} { try ; catch error throw 'Unlimited Blade Works' error; } { try ; catch error console; } ;
you'll get:
MyError: Unlimited Blade Works at wrapErrorWithMyError Error: Have withstood Pain to create many Weapons at throwBuiltinError at wrapErrorWithMyError at main at Object.<anonymous> at Module._compile at Object.Module._extensions..js at Module.load at tryModuleLoad at Function.Module._load at Function.Module.runMain
Special Thanks
- @yszk0123 as a reviewer
- bjyoungblood/es6-error as a reference
License
The module is available as open source under the terms of the MIT License.