https://github.com/schonmann/errorist
A JavaScript library that provides traceable errors ✨🕵️
https://github.com/schonmann/errorist
contributions-welcome error-handling errors javascript nodejs npm-package open-source typescript
Last synced: 8 months ago
JSON representation
A JavaScript library that provides traceable errors ✨🕵️
- Host: GitHub
- URL: https://github.com/schonmann/errorist
- Owner: schonmann
- License: mit
- Created: 2022-10-22T00:56:33.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-28T01:25:23.000Z (about 3 years ago)
- Last Synced: 2024-08-08T23:55:14.452Z (over 1 year ago)
- Topics: contributions-welcome, error-handling, errors, javascript, nodejs, npm-package, open-source, typescript
- Language: TypeScript
- Homepage:
- Size: 146 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# errorist 
A JavaScript library that provides traceable errors ✨🕵️
> One who encourages and propagates error.
>
> [— Wikitionary](https://en.wiktionary.org/wiki/errorist)
[TL;DR: check use cases!](#Usage)
## Install
The package for `errorist` is available on the public *npm registry*.
* With `yarn`:
```
yarn add errorist.js
```
* With `npm`:
```
npm install --save errorist.js
```
## Purpose
The library is designed to enforce a better context to an error by:
* Keeping track of an error root causes and detect them at ease;
* Forcing the usage of error codes;
* Encouraging succint, precise error messages;
* Patching errors with related data.
## Usage
### #1: Custom error class definition
```javascript
class SomeError extends Errorist {
constructor(message) {
super(message || "some human readable message");
this.code = "some-error-code";
this.name = "SomeError";
}
}
// Alternatively:
const SomeError = Errorist.extend({
message: "some human readable message",
code: "some-error-code",
name: "SomeError"
})
```
### #2: Throwing a custom error
```javascript
try {
// ...
} catch(e) {
throw SomeCustomError.create({
causes: e,
data: {
foo: "some data",
bar: "some other data"
}
})
}
```
### #3: Checking for causes
```javascript
const doSomething = () => {
throw SomeError.create({
causes: [new CauseOneError(), new CauseTwoError()]
});
};
try {
doSomething();
} catch (e) {
const err = Errorist.wrap(e);
err.is(SomeError) // => SomeError [Errorist]
err.isCausedBy(SomeError) // => null
err.isCausedBy(CauseOneError) // => CauseOneError [Errorist]
err.is(CauseOneError) // => CauseOneError [Errorist]
}
```
### #4: Out of the box, Golang style error handling
```javascript
const [err, thing] = await errorist.try(createThing);
if (err?.is(ThingAlreadyCreatedError)) {
// ...
}
```
## Maintainers ✨
- Antonio Schönmann ([@schonmann](https://github.com/schonmann))