dynamic-error
TL/DR: A dynamic error class with support for TypeScript, ES6, and old-style JavaScript supporting even very old browsers.
This package provides the abstract class DynamicError
. You can use it define error classes, that compute their error message dynamically from the internal state of the error object. For example, you can define an InvalidValueError<T>
that takes a value
argument in its constructor and computes the error message as you wish. It is derived from ExtendableError
from my ts-error
package, which enables the proper inheritance hierarchy (subclassing Error
normally does not work properly in JS).
The package is well-tested, very small and only has one production dependency (ts-error), which itself has no production dependencies.
Install
You can install the dynamic-error package from NPM with the command:
npm install dynamic-error
If you're using TypeScript, you will also need the @types/node
dev dependency (unless you have type checking disabled).
npm install --save-dev @types/node
Usage
You subclass DynamicError
, call super()
in the constructor, define the method errorMessage()
and then you can use your new error class in your code. If you want to pass state parameters to the constructor, you must call super
with a function argument, in which you can set the state of your error object (this is due to the JS quirks with Error objects).
In TypeScript:
; // A subclass of DynamicError, here also using generics. // Another subclass try catch e
In ES6 / esnext:
; // A subclass of DynamicError { // You need to use function(){} here and not arrow notation, because of `this`, which is not supported by arrow functions. super { thisvalue = value; }; } { return `The value is invalid.`; } // This method is not required, but I use it to demonstrate dynamic recalculation. { thisvalue = value; // this.update() recomputes the error message and stack trace. // The stack trace remains the same it was at error creation, only the message in the stack trace is updated. this; } // Another subclass public { return `The number is invalid.`; } try throw -1; throw "abc"; catch e if e instanceof NumberValueError // ... else // ...
In ES5:
var DynamicError = DynamicError; // This is taken from TypeScript compiler output, because it works quite reliably.// There are various other methods though, so use whatever you like, if you have to use ES5.var __extends = this && this__extends || { var extendStatics = ObjectsetPrototypeOf || __proto__: instanceof Array && { d__proto__ = b; } || { for var p in b if b dp = bp; }; return { ; { thisconstructor = d; } dprototype = b === null ? Object : __prototype = bprototype ; };}; var ValueError = /** @class */ { ; { return _super !== null && _super || this; } ValueErrorprototype { return "The value " + thisvalue + " is invalid."; }; ValueErrorprototype { thisvalue = value; this; }; return ValueError;}DynamicError; var NumberValueError = /** @class */ { ; { return _super !== null && _super || this; } NumberValueErrorprototype { return "The number " + thisvalue + " is invalid."; }; return NumberValueError;}ValueError; try throw -1; throw "abc"; catch e if e instanceof NumberValueError // ... else // ...
Tests
Run npm run test
to run the mocha/chai test suite. The ExtendableError package is well-tested in node and all browsers, including very old versions, so this Error class will be compatible even with very old and mobile browsers.
If you encounter any issues, please file an issue and I will investigate and fix it.
License
MIT (see ./LICENSE).