@alius/exception
v1.0.5
Published
JS Error with cause
Downloads
5
Readme
Extension of Error class allows chain of errors.
Installation
npm install @alius/exception
Usage
Constructor accepts two parameters (optional): message and cause.
import { Exception } from "@alius/exception";
function foo() {
throw new Exception("Error in foo");
}
function bar() {
try {
foo();
} catch (err) {
throw new Exception("Error in bar", err);
}
}
try {
bar();
} catch (err) {
console.log(err);
}
Serialization
Exception object can easily serialized to JSON with JSON.stringify()
.
Following properties are serialized (if present):
- name
- message
- code
- data
- signal
- cause (recursively). If property value is function - it is executed to acquire actual value.
toObject()
Class has method err.toObject()
which returns serialized error object with stack
property
containing stack trace.
Exception.errSerializer(err, showStack = true)
Static method to serialize any Error object.