Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeroenptrs/assert-errors
❗️A library attempting to make errors a bit more typed
https://github.com/jeroenptrs/assert-errors
Last synced: 1 day ago
JSON representation
❗️A library attempting to make errors a bit more typed
- Host: GitHub
- URL: https://github.com/jeroenptrs/assert-errors
- Owner: jeroenptrs
- Created: 2023-08-27T17:17:23.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-12T18:47:54.000Z (about 2 months ago)
- Last Synced: 2024-10-13T17:10:18.892Z (25 days ago)
- Language: TypeScript
- Size: 73.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
❗️ assert-errors
A library attempting to make errors a bit more typed
## Goal
The goal of this package is to provide runtime checks against one or more defined Error classes, while wrapping around a normal or async function.
It will return a tuple of type [result, error].
If the wrapped function doesn't throw, result will be defined. If it does throw, error will be returned.
If the wrapped function returns an error that does not match the checks, it will throw an `UnknownError`, which contains the originalError `e.originalError`.## Installation
`yarn add @jeroenpeeters/assert-errors`
## API
### sync
For normal functions, we provide the following APIs - `assertErrors, assertErrorsCurry, asserErrorsOnce`:
```typescript
import { assertErrors, assertErrorsCurry, asserErrorsOnce } from "@jeroenpeeters/assert-errors";const wrappedFunction = assertErrors(MyError, myFunc);
const wrappedFunction = assertErrors([MyError, MyOtherError], myFunc);const assertMyError = assertErrorsCurry(MyError);
const wrappedFunction = assertMyError(myFunc);
const assertMyErrors = assertErrorsCurry([MyError, MyOtherError]);
const wrappedFunction = assertMyErrors(myFunc);const [result, error] = wrappedFunction(myArg, myOtherArg);
const [result, error] = assertErrorsOnce(MyError, myFunc, myArg, myOtherArg);
const [result, error] = assertErrorsOnce([MyError, MyOtherError], myFunc, myArg, myOtherArg);
```### async
For async functions, we provide these APIs - `assertErrorsAsync, assertErrorsCurryAsync, asserErrorsOnceAsync`:
```typescript
import {
assertErrorsAsync,
assertErrorsCurryAsync,
asserErrorsOnceAsync,
} from "@jeroenpeeters/assert-errors";const wrappedFunction = assertErrorsAsync(MyError, myAsyncFunc);
const wrappedFunction = assertErrorsAsync([MyError, MyOtherError], myAsyncFunc);const assertMyError = assertErrorsCurryAsync(MyError);
const wrappedFunction = assertMyError(myAsyncFunc);
const assertMyErrors = assertErrorsCurryAsync([MyError, MyOtherError]);
const wrappedFunction = assertMyErrors(myAsyncFunc);const [result, error] = await wrappedFunction(myArg, myOtherArg);
const [result, error] = await asserErrorsOnceAsync(MyError, myAsyncFunc, myArg, myOtherArg);
const [result, error] = await asserErrorsOnceAsync(
[MyError, MyOtherError],
myAsyncFunc,
myArg,
myOtherArg
);
```