ignor
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

ignor

Ignore errors conditionally in sync / async / promise functions and return a default value.

Synopsis

import { ignoreCode } from "ignor";

// Async: Returns "default value" if thrown error code is "ECONNREFUSED", otherwise throws.
await got(url).catch(ignoreCode("ECONNREFUSED", "default value"));

// AggregateError: Returns "default value" if all errors of an aggregate error are ignored.
await Promise.any([got(url), got(url)]).catch(ignoreCode("ECONNREFUSED", "default value"));

// Sync: Returns "default value" if thrown error code is "ENOENT", otherwise throws.
ignoreCode("ENOENT", "default value", () => readFileSync("x.txt"));

Async

// Ignore an error with a code and return undefined. Otherwise throw error.
await got(url).catch(ignoreCode("ECONNREFUSED"));

// Ignore an error with a code and return default value.
await got(url).catch(ignoreCode("ECONNREFUSED", "no content"));

// Ignore an error with multiple codes.
await got(url).catch(ignoreCode(["ENOTDIR", "ENOENT"]));

// Ignore an error with a message.
await got(url).catch(ignoreMessage(/cannot connect/));

// Ignore an error with a status.
await got(url).catch(ignoreStatus(404));

Sync

// An example sync function.
const func = () => readFileSync("x.txt");

// Ignore an error with a code and return undefined. Otherwise throw error.
ignore.code("ENOENT", func);

// Ignore an error with a code and return default value.
ignore.code("ENOENT", "no content", func);

// Ignore an error with multiple codes.
ignore.code(["ENOTDIR", "ENOENT"], func);

// Ignore an error with a message.
ignore.message(/cannot connect/, func);

// Ignore an error with a status.
ignore.status(404, func);

API

ignor

ignor

Table of contents

Functions

Functions

ignoreCode

ignoreCode<R>(ignore: Multi<Ignore> | undefined, fn: (...args: any[]) => R): undefined | R

Ignores errors with the given code in sync functions.

Example

import * as ignore from "ignor";
ignore.code("ENOENT", () => readFileSync("file.txt"));
ignore.code(["ENOENT", "OTHER"], () => readFileSync("file.txt"));

thorws if error is not one of the ignored codes.

Type parameters:

Name Description
R is the return type of the executed function.

Parameters:

Name Type Description
ignore Multi<Ignore> | undefined is the code or codes to ignore.
fn (...args: any[]) => R is the function to execute and ignore some of the errors.

Returns: undefined | R

undefined.

Defined in: ignore.ts:20

ignoreCode<D, R>(ignore: Multi<Ignore> | undefined, defaultValue: D, fn: (...args: any[]) => R): D | R

Ignores errors with the given code in sync functions.

Example

import * as ignore from "ignor";
ignore.code("ENOENT", "default", () => readFileSync("file.txt"));
ignore.code(["ENOENT", "OTHER"], "default", () => readFileSync("file.txt"));

thorws if error is not one of the ignored codes.

Type parameters:

Name Description
D is the type of returned default value if an error is ignored.
R is the return type of the executed function.

Parameters:

Name Type Description
ignore Multi<Ignore> | undefined is the code or codes to ignore.
defaultValue D is the default value to return if an error is ignored.
fn (...args: any[]) => R is the function to execute and ignore some of the errors.

Returns: D | R

undefined.

Defined in: ignore.ts:39

ignoreCode(ignore?: Multi<Ignore>): (e: Error) => undefined

Ignores errors with the given code in async functions.

Example

import * as ignore from "ignor";
await got(url).catch(ignore.code("ECONNREFUSED"));
await got(url).catch(ignore.code(["ECONNREFUSED", "OTHER"]));

thorws if error is not one of the ignored codes.

Parameters:

Name Type Description
ignore? Multi<Ignore> is the code or codes to ignore.

Returns: function

undefined.

Defined in: ignore.ts:53

ignoreCode<D>(ignore: Multi<Ignore> | undefined, defaultValue: D): (e: Error) => D

Ignores errors with the given code in async functions.

Example

import * as ignore from "ignor";
await got(url).catch(ignore.code("ECONNREFUSED", []));
await got(url).catch(ignore.code(["ECONNREFUSED", "OTHER"], []));

thorws if error is not one of the ignored codes.

Type parameters:

Name Description
D is the type of returned default value if an error is ignored.

Parameters:

Name Type Description
ignore Multi<Ignore> | undefined is the code or codes to ignore.
defaultValue D is the default value to return if an error is ignored.

Returns: function

default value.

Defined in: ignore.ts:70


ignoreMessage

ignoreMessage<R>(ignore: Multi<Ignore> | undefined, fn: (...args: any[]) => R): undefined | R

Type parameters:

Name
R

Parameters:

Name Type
ignore Multi<Ignore> | undefined
fn (...args: any[]) => R

Returns: undefined | R

Defined in: ignore.ts:75

ignoreMessage<D, R>(ignore: Multi<Ignore> | undefined, defaultValue: D, fn: (...args: any[]) => R): D | R

Type parameters:

Name
D
R

Parameters:

Name Type
ignore Multi<Ignore> | undefined
defaultValue D
fn (...args: any[]) => R

Returns: D | R

Defined in: ignore.ts:76

ignoreMessage(ignore?: Multi<Ignore>): (e: Error) => undefined

Parameters:

Name Type
ignore? Multi<Ignore>

Returns: function

Defined in: ignore.ts:77

ignoreMessage<D>(ignore: Multi<Ignore> | undefined, defaultValue: D): (e: Error) => D

Type parameters:

Name
D

Parameters:

Name Type
ignore Multi<Ignore> | undefined
defaultValue D

Returns: function

Defined in: ignore.ts:78


ignoreStatus

ignoreStatus<R>(ignore: Multi<Ignore> | undefined, fn: (...args: any[]) => R): undefined | R

Type parameters:

Name
R

Parameters:

Name Type
ignore Multi<Ignore> | undefined
fn (...args: any[]) => R

Returns: undefined | R

Defined in: ignore.ts:83

ignoreStatus<D, R>(ignore: Multi<Ignore> | undefined, defaultValue: D, fn: (...args: any[]) => R): D | R

Type parameters:

Name
D
R

Parameters:

Name Type
ignore Multi<Ignore> | undefined
defaultValue D
fn (...args: any[]) => R

Returns: D | R

Defined in: ignore.ts:84

ignoreStatus(ignore?: Multi<Ignore>): (e: Error) => undefined

Parameters:

Name Type
ignore? Multi<Ignore>

Returns: function

Defined in: ignore.ts:85

ignoreStatus<D>(ignore: Multi<Ignore> | undefined, defaultValue: D): (e: Error) => D

Type parameters:

Name
D

Parameters:

Name Type
ignore Multi<Ignore> | undefined
defaultValue D

Returns: function

Defined in: ignore.ts:86

Package Sidebar

Install

npm i ignor

Weekly Downloads

4

Version

1.2.0

License

MIT

Unpacked Size

120 kB

Total Files

28

Last publish

Collaborators

  • ozum