@huds0n/error
v2.0.0-beta1
Published
Extended JS Error
Downloads
44
Readme
📝 Table of Contents
🧐 About
Error Class extending on JavaScript's Error, adding in additional properties, such as: code, severity, handled, ect...
Used throughout the Huds0n modules libary.
✅ List of Features
- Informative: Adds typed props for code, severity, info, and more.
- Current: Easily updated or flagged as handled.
- Lossless: Update history is stored.
- Typed: Fully integrated with typescript out-of-the-box.
- Transferable: Built-in methods to JSON stringify and parse.
🏁 Getting Started
Installing
npm i @huds0n/error
🧑💻 Basic Usage
Creating an Error
import { Huds0nError } from '@huds0n/error';
// Or with node
const { HudsonError } = require('@huds0n/error');
const exampleError = new Huds0nError(errorProps);
See reference for errorProps
Handling an Error
exampleError.handled(handledProps);
Handled props is an optional object that adds handled info to the error.
Updating an Error
exampleError.update(props);
The new properties are added to the error. Overwritten properties are pushed to the update history, which can be seen on error printing.
Displaying an Error
For concise logging use the toString method.
console.log(exampleError.toString());
/*
Huds0nError - EXAMPLE_ERROR (MEDIUM - HANDLED)
*/
For detailed information use the print method.
console.log(exampleError.print());
/*
---------------------------------------------------------------
Huds0nError - EXAMPLE_ERROR (MEDIUM - HANDLED)
Info: {
...info is stringified here
}
HandledInformation: {
...if error's handled flag is an object it is stringified here
}
UpdateHx: [...array of overwritten error properties with timestamps]
...stack trace
---------------------------------------------------------------
*/
Transforming an Error
Sometimes may need to convert an error into a Huds0nError, i.e. a catch block. For this, use the static transform method.
try {
// Some code
} catch (e) {
const caughtError = Huds0nError.transform(e, errorProps);
}
The transform error converts any type into a Huds0nError. ErrorProps are then merged to create the Huds0nError, with additional information being placed in the info prop. If the error being transformed is already a Huds0nError it is left unchanged with only the info props being merged.
🧑🔬 Advanced Usage
Reviving an Error
HudsOnErrors can easily be turned into JSON with the instance method toJSON, then parsed back to a Huds0nError using the static method JSONreviver
const stringifiedError = exampleError.toJSON();
const revivedError = JSON.parse(stringifiedError, Huds0nError.JSONreviver);
📖 Reference
Error Props
| Prop | Required/(Default) | Description | Type | | -------- | :-------------------------: | ----------------------------------------- | -------------------------------------- | | code | ✔ | user defined error code | string or number | | handled | false | whether error has been handle | boolean or object | | info | {} | additional information | object | | message | Message Missing | human-readable error message | string | | name | Huds0nError | error nameuseful for grouping errors | string | | severity | ✔ | severity of error | HIGH, MEDIUM,LOW, or NONE | | stack | (Inherited from JS Error) | trace of error | string |
✍️ Authors
- @JontiHudson - Idea & Initial work
- @MartinHudson - Support & Development
See also the list of contributors who participated in this project.
🎉 Acknowledgements
- Special thanks to my fiance, Arma, who has been so patient with all my extra-curricular work.