modern-errors
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -36,4 +36,4 @@ import { ErrorName, OnCreate, ErrorType, ErrorParams } from 'error-type' | ||
/** | ||
* Any error type can be retrieved from the return value, such as | ||
* `InputError`. | ||
* Any error type can be retrieved from the return value. | ||
* For example, `InputError`, `AuthError`, etc. | ||
* | ||
@@ -40,0 +40,0 @@ * @example |
{ | ||
"name": "modern-errors", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"type": "module", | ||
@@ -14,3 +14,3 @@ "exports": "./build/src/main.js", | ||
}, | ||
"description": "Handle errors like it's 2022", | ||
"description": "Handle errors like it's 2022 🔮", | ||
"keywords": [ | ||
@@ -36,3 +36,3 @@ "nodejs", | ||
"create", | ||
"normalization" | ||
"framework" | ||
], | ||
@@ -52,5 +52,5 @@ "license": "Apache-2.0", | ||
"error-cause": "^1.0.4", | ||
"error-type": "^1.1.8", | ||
"error-type": "^1.1.9", | ||
"is-plain-obj": "^4.1.0", | ||
"merge-error-cause": "^1.1.2" | ||
"merge-error-cause": "^1.1.3" | ||
}, | ||
@@ -57,0 +57,0 @@ "devDependencies": { |
@@ -0,1 +1,3 @@ | ||
<img src="https://raw.githubusercontent.com/ehmicky/design/main/modern-errors/modern-errors.svg" width="600"/> | ||
[![Codecov](https://img.shields.io/codecov/c/github/ehmicky/modern-errors.svg?label=tested&logo=codecov)](https://codecov.io/gh/ehmicky/modern-errors) | ||
@@ -7,3 +9,3 @@ [![Build](https://github.com/ehmicky/modern-errors/workflows/Build/badge.svg)](https://github.com/ehmicky/modern-errors/actions) | ||
Handle errors like it's 2022. | ||
Handle errors like it's 2022 🔮 | ||
@@ -16,12 +18,14 @@ Error handling framework that is minimalist yet featureful. | ||
- [Custom error types](https://github.com/ehmicky/modern-errors#create-error-types-and-handler) | ||
- Wrap any error's [message](#wrap-error-message), [type](#set-error-type) or | ||
- Wrap any error's [message](#wrap-error-message), [type](#set-error-type), or | ||
[properties](#wrap-error-properties) | ||
- Set properties on [new errors](#set-error-properties) or on | ||
- Set properties on [individual errors](#set-error-properties), or on | ||
[all errors of the same type](#error-type-properties) | ||
- Automatically separate [system errors/bugs](#system-errors) from user errors | ||
- Automatically separate (unhandled) [system errors](#system-errors) from | ||
(handled) user errors | ||
- System errors indicate where to [report bugs](#bug-reports) | ||
- Handle [invalid errors](#invalid-errors) (not an `Error` instance, missing | ||
stack, etc.) | ||
- Follows error handling | ||
- Follow error handling | ||
[best practices](https://github.com/ehmicky/error-type#best-practices) | ||
- Based on [modern JavaScript features](https://github.com/es-shims/error-cause) | ||
@@ -54,3 +58,3 @@ # Example | ||
Throw/rethrow errors. | ||
Throw/re-throw errors. | ||
@@ -95,3 +99,4 @@ ```js | ||
Any error type [can be retrieved](#create-error-types-and-handler) from the | ||
return value, such as `InputError`. | ||
return value. The name must end with `Error`. For example: `InputError`, | ||
`AuthError`, etc. | ||
@@ -116,4 +121,4 @@ #### errorHandler | ||
Called on any `new ErrorType('message', parameters)`. Can be used to | ||
[customize error parameters](#customize-error-parameters) or to set | ||
Called on any [`new ErrorType('message', parameters)`](#set-error-properties). | ||
Can be used to [customize error parameters](#customize-error-parameters) or set | ||
[error type properties](#error-type-properties). By default, any `parameters` | ||
@@ -128,3 +133,5 @@ are [set as error properties](#set-error-properties). | ||
✨ Retrieving the error types automatically creates them. ✨ | ||
✨ Retrieving the error types | ||
[automatically](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) | ||
creates them. ✨ | ||
@@ -135,2 +142,3 @@ ```js | ||
// The error types are examples: any name ending with "Error" can be specified | ||
export const { errorHandler, InputError, AuthError, DatabaseError } = | ||
@@ -142,3 +150,3 @@ modernErrors() | ||
Each main function should be wrapped with the [`errorHandler`](#errorhandler). | ||
Each main function should be wrapped with the [`errorHandler()`](#errorhandler). | ||
@@ -175,5 +183,6 @@ ```js | ||
Invalid errors [are normalized](https://github.com/ehmicky/normalize-exception) | ||
by `errorHandler`. This includes errors that are not an | ||
by [`errorHandler()`](#error-handler). This includes errors that are not an | ||
[`Error` instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | ||
or that have wrong/missing properties. | ||
or that have | ||
[wrong/missing properties](https://github.com/ehmicky/normalize-exception#examples). | ||
@@ -199,3 +208,3 @@ <!-- eslint-disable no-throw-literal --> | ||
This allows wrapping the error [message](#wrap-error-message), | ||
[properties](#wrap-error-properties) and [type](#set-error-type). | ||
[properties](#wrap-error-properties), or [type](#set-error-type). | ||
@@ -214,3 +223,3 @@ ```js | ||
The [`errorHandler`](#error-handler) | ||
The [`errorHandler()`](#error-handler) | ||
[merges all error `cause`](https://github.com/ehmicky/merge-error-cause) into a | ||
@@ -224,7 +233,7 @@ single error, including their | ||
- There is no need to | ||
[traverse `error.cause`](https://github.com/ehmicky/merge-error-cause#traversing-errorcause) | ||
- The | ||
[stack trace](https://github.com/ehmicky/merge-error-cause#verbose-stack-trace) | ||
is simple while still keeping all information | ||
- `error.cause` does not need to be | ||
[traversed](https://github.com/ehmicky/merge-error-cause#traversing-errorcause) | ||
- The stack trace is neither | ||
[verbose nor redundant](https://github.com/ehmicky/merge-error-cause#verbose-stack-trace), | ||
while still keeping all information | ||
@@ -245,3 +254,3 @@ ### Wrap error message | ||
If the outer error message ends with `:`, that message is prepended instead. | ||
If the outer error message ends with `:`, it is prepended instead. | ||
@@ -320,2 +329,26 @@ ```js | ||
### Type-specific logic | ||
The [`onCreate()` option](#oncreate) can trigger error type-specific logic. | ||
<!-- eslint-disable n/handle-callback-err --> | ||
```js | ||
modernErrors({ | ||
onCreate(error, parameters) { | ||
onCreateError[error.name](error, parameters) | ||
}, | ||
}) | ||
const onCreateError = { | ||
InputError(error, parameters) { | ||
// ... | ||
}, | ||
AuthError(error, parameters) { | ||
// ... | ||
}, | ||
// ... | ||
} | ||
``` | ||
### Error type properties | ||
@@ -353,7 +386,7 @@ | ||
Once [`errorHandler`](#error-handler) has been applied, the error type can be | ||
Once [`errorHandler()`](#error-handler) has been applied, the error type can be | ||
checked by its `name` (as opposed to | ||
[`instanceof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof)). | ||
Libraries should document their possible error names, but do not need to | ||
`export` the error types. | ||
`export` their error types. | ||
@@ -396,6 +429,8 @@ ```js | ||
System errors/bugs can be distinguished from user errors by handling any user | ||
errors in `try {} catch {}` and [re-throwing](#re-throw-errors) them | ||
[with an error type](#set-error-type). The [`errorHandler`](#error-handler) | ||
assigns the `SystemError` type to any error with an unknown type. | ||
System errors/bugs can be distinguished from user errors by | ||
[handling any possible errors](#re-throw-errors) in `try {} catch {}` and | ||
[re-throwing](#re-throw-errors) them | ||
[with a specific error type](#set-error-type). The | ||
[`errorHandler()`](#error-handler) assigns the `SystemError` type to any error | ||
with an unknown type. | ||
@@ -414,3 +449,3 @@ <!-- eslint-disable unicorn/no-null --> | ||
If the `bugsUrl` option is used, | ||
If the [`bugsUrl` option](#bugsurl) is used, | ||
@@ -417,0 +452,0 @@ ```js |
42567
487
Updatederror-type@^1.1.9
Updatedmerge-error-cause@^1.1.3