New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@jeroenpeeters/assert-errors

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jeroenpeeters/assert-errors

A library attempting to make errors a bit more typed

1.0.2
latest
Source
npm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

❗️ 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:

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:

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
);

Keywords

typescript

FAQs

Package last updated on 29 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts