modern-errors
Advanced tools
Comparing version 5.1.1 to 5.2.0
@@ -38,2 +38,4 @@ import type { ErrorInstance } from './merge/cause/main.js' | ||
// - Plugin methods cannot be generic | ||
// - Plugin types can use `ErrorClass` or `Info`, but not export them. | ||
// See the comment in info.d.ts for an explanation. | ||
// Medium limitations: | ||
@@ -40,0 +42,0 @@ // - Some logic relies on determining if an error class is a subclass of |
{ | ||
"name": "modern-errors", | ||
"version": "5.1.1", | ||
"version": "5.2.0", | ||
"type": "module", | ||
@@ -19,3 +19,3 @@ "exports": { | ||
}, | ||
"description": "Handle errors like it's 2023 🔮", | ||
"description": "Handle errors in a simple, stable, consistent way", | ||
"keywords": [ | ||
@@ -60,5 +60,5 @@ "nodejs", | ||
"is-plain-obj": "^4.1.0", | ||
"merge-error-cause": "^3.3.0", | ||
"merge-error-cause": "^3.4.0", | ||
"normalize-exception": "^2.11.0", | ||
"set-error-message": "^1.5.0", | ||
"set-error-message": "^1.6.0", | ||
"set-error-props": "^3.5.0", | ||
@@ -73,7 +73,7 @@ "set-error-stack": "^1.0.0" | ||
"modern-errors-cli": "^2.2.0", | ||
"modern-errors-http": "^2.2.0", | ||
"modern-errors-http": "^2.3.0", | ||
"modern-errors-process": "^2.1.0", | ||
"modern-errors-serialize": "^3.0.0", | ||
"modern-errors-switch": "^1.0.0", | ||
"modern-errors-winston": "^2.2.0", | ||
"modern-errors-switch": "^1.0.1", | ||
"modern-errors-winston": "^2.3.0", | ||
"test-each": "^5.6.0" | ||
@@ -80,0 +80,0 @@ }, |
111
README.md
@@ -11,26 +11,21 @@ <picture> | ||
[![Minified size](https://img.shields.io/bundlephobia/minzip/modern-errors?label&colorA=404040&colorB=808080&logo=webpack)](https://bundlephobia.com/package/modern-errors) | ||
[![Twitter](https://img.shields.io/badge/-Twitter-808080.svg?logo=twitter&colorA=404040)](https://twitter.com/intent/follow?screen_name=ehmicky) | ||
[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon&colorA=404040&logoColor=9590F9)](https://fosstodon.org/@ehmicky) | ||
[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium&colorA=404040)](https://medium.com/@ehmicky) | ||
Handle errors like it's 2023 🔮 | ||
Handle errors in a simple, stable, consistent way. | ||
Error handling framework that is pluggable, minimalist yet featureful. | ||
# Features | ||
- ⛑️ Create [error classes](#create-error-classes) (including with | ||
[custom logic](#-custom-logic)) | ||
- 🏷️ Set properties on [individual errors](#error-instance-properties) or on | ||
[all errors of the same class](#error-class-properties) | ||
- 🎀 Wrap errors' [message](#wrap-error-message), [class](#wrap-error-class) and | ||
[properties](#wrap-error-options) | ||
- 🚨 Normalize [invalid errors](#invalid-errors) (not an `Error` instance, | ||
missing `stack`, etc.) | ||
- 🐞 Separate known and [unknown errors](#-unknown-errors) | ||
Simple patterns to: | ||
- ⛑️ Create error [classes](#create-error-classes) | ||
- 🏷️ Set error [properties](#error-instance-properties) | ||
- 🎀 [Wrap](#-wrap-errors) or [aggregate](#aggregate-errors) errors | ||
- 🐞 Separate known and [unknown](#-unknown-errors) errors | ||
Stability: | ||
- 🚨 [Normalize](#-normalize-errors) invalid errors | ||
- 🛡️ 100% [test coverage](https://app.codecov.io/gh/ehmicky/modern-errors) | ||
- 🤓 Strict [TypeScript types](docs/typescript.md) | ||
- 📖 Based on standard JavaScript: [`throw`](#%EF%B8%8F-throw-errors), | ||
[`try/catch`](#-wrap-errors), [`new Error()`](#%EF%B8%8F-throw-errors), | ||
[`error.cause`](#-wrap-errors), [`instanceof`](#check-error-classes), | ||
[`class`](#-custom-logic), | ||
[`toJSON()`](https://github.com/ehmicky/modern-errors-serialize#errortojson) | ||
@@ -156,5 +151,4 @@ # Plugins | ||
Exporting and documenting all error classes (except for | ||
[`ModernError`](#modernerror)) allows consumers to check them. This also enables | ||
sharing error classes between modules. | ||
Exporting and documenting all error classes allows consumers to check them. This | ||
also enables sharing error classes between modules. | ||
@@ -173,3 +167,3 @@ ### Check error classes | ||
[subclass](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends). | ||
Parent classes' [options](#options) are merged to their subclasses. | ||
Parent classes' [options](#options) are merged with their subclasses. | ||
@@ -377,31 +371,28 @@ ```js | ||
### Top-level error handler | ||
## 🐞 Unknown errors | ||
Wrapping a module's main functions with | ||
[`BaseError.normalize(error, UnknownError)`](#errorclassnormalizeerror-newerrorclass) | ||
ensures every error being thrown is [valid](#invalid-errors), applies | ||
[plugins](#-plugins), and has a class that is either | ||
[_known_](#create-error-classes) or [`UnknownError`](#-unknown-errors). | ||
### Handling known errors | ||
Known errors should be handled in a `try {} catch {}` block and | ||
[wrapped](#wrap-error-class) with a [specific class](#create-error-classes). | ||
That block should only cover the statement that might throw in order to prevent | ||
catching other unrelated errors. | ||
<!-- eslint-skip --> | ||
```js | ||
export const main = function () { | ||
try { | ||
// ... | ||
} catch (error) { | ||
throw BaseError.normalize(error, UnknownError) | ||
} | ||
try { | ||
return regExp.test(value) | ||
} catch (error) { | ||
// Now an `InputError` instance | ||
throw new InputError('Invalid regular expression:', { cause: error }) | ||
} | ||
``` | ||
## 🐞 Unknown errors | ||
### Normalizing unknown errors | ||
An error is _unknown_ if its class is not a [subclass](#error-subclasses) of the | ||
[`BaseError`](#create-error-classes). This indicates an unexpected exception, | ||
usually a bug. | ||
If an error is not handled as described [above](#handling-known-errors), it is | ||
considered _unknown_. This indicates an unexpected exception, usually a bug. | ||
[`BaseError.normalize(error, UnknownError)`](#errorclassnormalizeerror-newerrorclass) | ||
assigns the `UnknownError` class to those errors. This is usually performed in | ||
the [top-level error handler](#top-level-error-handler). | ||
assigns the `UnknownError` class to those errors. | ||
@@ -423,17 +414,17 @@ ```js | ||
### Handling unknown errors | ||
### Top-level error handler | ||
_Unknown_ errors should be handled in a `try {} catch {}` block and | ||
[wrapped](#wrap-error-class) with a [_known_ class](#create-error-classes) | ||
instead. That block should only cover the statement that might throw in order to | ||
prevent catching other unrelated _unknown_ errors. | ||
Wrapping a module's main functions with | ||
[`BaseError.normalize(error, UnknownError)`](#errorclassnormalizeerror-newerrorclass) | ||
ensures every error being thrown is [valid](#invalid-errors), applies | ||
[plugins](#-plugins), and has a class that is either | ||
[_known_](#create-error-classes) or [`UnknownError`](#-unknown-errors). | ||
<!-- eslint-skip --> | ||
```js | ||
try { | ||
return regExp.test(value) | ||
} catch (error) { | ||
// Now an `InputError` instance | ||
throw new InputError('Invalid regular expression:', { cause: error }) | ||
export const main = function () { | ||
try { | ||
// ... | ||
} catch (error) { | ||
throw BaseError.normalize(error, UnknownError) | ||
} | ||
} | ||
@@ -623,4 +614,4 @@ ``` | ||
`error`: `any`\ | ||
`NewErrorClass`: `subclass of ErrorClass`\ | ||
`error`: `Error | any`\ | ||
`NewErrorClass`: subclass of `ErrorClass`\ | ||
_Return value_: `Error` | ||
@@ -655,2 +646,4 @@ | ||
update an error's message | ||
- [`wrap-error-message`](https://github.com/ehmicky/wrap-error-message): | ||
Properly wrap an error's message | ||
- [`set-error-props`](https://github.com/ehmicky/set-error-props): Properly | ||
@@ -664,2 +657,6 @@ update an error's properties | ||
some ❤ to Node.js process errors | ||
- [`error-http-response`](https://github.com/ehmicky/error-http-response): | ||
Create HTTP error responses | ||
- [`winston-error-format`](https://github.com/ehmicky/winston-error-format): Log | ||
errors with Winston | ||
@@ -690,4 +687,4 @@ # Support | ||
<!-- | ||
<table><tr><td align="center"><a href="https://twitter.com/ehmicky"><img src="https://avatars2.githubusercontent.com/u/8136211?v=4" width="100px;" alt="ehmicky"/><br /><sub><b>ehmicky</b></sub></a><br /><a href="https://github.com/ehmicky/modern-errors/commits?author=ehmicky" title="Code">💻</a> <a href="#design-ehmicky" title="Design">🎨</a> <a href="#ideas-ehmicky" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ehmicky/modern-errors/commits?author=ehmicky" title="Documentation">📖</a></td></tr></table> | ||
<table><tr><td align="center"><a href="https://fosstodon.org/@ehmicky"><img src="https://avatars2.githubusercontent.com/u/8136211?v=4" width="100px;" alt="ehmicky"/><br /><sub><b>ehmicky</b></sub></a><br /><a href="https://github.com/ehmicky/modern-errors/commits?author=ehmicky" title="Code">💻</a> <a href="#design-ehmicky" title="Design">🎨</a> <a href="#ideas-ehmicky" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ehmicky/modern-errors/commits?author=ehmicky" title="Documentation">📖</a></td></tr></table> | ||
--> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> |
2211
91381
682
Updatedmerge-error-cause@^3.4.0
Updatedset-error-message@^1.6.0