Huge News!Announcing our $40M Series B led by Abstract Ventures.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.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
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

FAQs

Package last updated on 28 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc