@service/error
v0.1.3
Published
service.js error package
Downloads
1
Readme
@service/error
@service/error
provides two shared error classes, which allow error classes to be extended and inheritance checks to be perfomed.
Useful links & documentation
- service.js Github repository
- service.js website
- Learn service.js (tutorials + examples)
- service.js documentation
- Documentation for this package (@service/error)
- Commercial support
- Automorph (the company behind service.js)
Installation
If you're using Yarn:
yarn add @service/error
If you're using the npm
command directly:
npm install @service/error
Usage
This package exports the classes DynamicError
and ExtendableError
.
ExtendableError
(from the ts-error
package) is a base error class that can be extended to achieve custom error classes with a working inheritance hierarchy, supporting all browsers (including very old versions) and of course node.
DynamicError
(from the dynamic-error
package) extends ExtendableError
and can(re-)compute its message and stack trace dynamically when its state changes. It is useful for error messages that dynamically depend on props passed in the error constructor.
import { DynamicError, ExtendableError } from "@service/error
class MyExtendableError extends ExtendableError {
constructor() {
super("An optional error message");
}
}
class MyDynamicError<T> extends DynamicError {
protected someVar: T;
constructor(someVar: T) {
super(() => {
this.someVar = someVar;
});
}
public errorMessage() {
return `Some error message depending on properties of this object, like someVar${this.someVar}`;
}
}
try {
throw new MyDynamicError(42);
} catch (e) {
if (e instanceof DynamicError) {
console.error("A DynamicError has been caught", e);
}
if (e instanceof ExtendableError) {
console.error("An ExtendableError has been caught. In this example both lines will thus get printed");
}
}
License: MIT
For the full license text, see ./LICENSE.