npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ignor

v1.2.0

Published

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

Downloads

19

Readme

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