1.2.0 • Published 3 years ago

ignor v1.2.0

Weekly downloads
118
License
MIT
Repository
github
Last release
3 years ago

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:

NameDescription
Ris the return type of the executed function.

Parameters:

NameTypeDescription
ignoreMulti<Ignore> | undefinedis the code or codes to ignore.
fn(...args: any[]) => Ris 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:

NameDescription
Dis the type of returned default value if an error is ignored.
Ris the return type of the executed function.

Parameters:

NameTypeDescription
ignoreMulti<Ignore> | undefinedis the code or codes to ignore.
defaultValueDis the default value to return if an error is ignored.
fn(...args: any[]) => Ris 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:

NameTypeDescription
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:

NameDescription
Dis the type of returned default value if an error is ignored.

Parameters:

NameTypeDescription
ignoreMulti<Ignore> | undefinedis the code or codes to ignore.
defaultValueDis 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:

NameType
ignoreMulti<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:

NameType
ignoreMulti<Ignore> | undefined
defaultValueD
fn(...args: any[]) => R

Returns: D | R

Defined in: ignore.ts:76

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

Parameters:

NameType
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:

NameType
ignoreMulti<Ignore> | undefined
defaultValueD

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:

NameType
ignoreMulti<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:

NameType
ignoreMulti<Ignore> | undefined
defaultValueD
fn(...args: any[]) => R

Returns: D | R

Defined in: ignore.ts:84

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

Parameters:

NameType
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:

NameType
ignoreMulti<Ignore> | undefined
defaultValueD

Returns: function

Defined in: ignore.ts:86