is-network-error
Check if a value is a Fetch network error
Last updated 2 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install is-network-error 
SYNC missed versions from official npm registry.

is-network-error

Check if a value is a Fetch network error

This can be useful when you want to do something specific when a network error happens without catching other Fetch-related errors.

Unfortunately, Fetch network errors are not standardized and differ among implementations. This package handles the differences across Node.js, Bun, Deno, and browsers.

For instance, p-retry uses this package to retry on network errors.

Install

npm install is-network-error

Usage

import isNetworkError from 'is-network-error';

async function getUnicorns() {
	try {
		const response = await fetch('unicorns.json');
		return await response.json();
	} catch (error) {
		if (isNetworkError(error)) {
			return localStorage.getItem('…');
		}

		throw error;
	}
}

console.log(await getUnicorns());

API

isNetworkError(value: unknown): value is TypeError

Returns true if the given value is a Fetch network error, otherwise false.

This function acts as a type guard, narrowing the type to TypeError when it returns true.

Current Tags

  • 1.3.1                                ...           latest (2 months ago)

6 Versions

  • 1.3.1                                ...           2 months ago
  • 1.3.0                                ...           7 months ago
  • 1.2.0                                ...           7 months ago
  • 1.1.0                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 38
This Month 56
Last Day 2
Last Week 25
Last Month 74
Dependencies (0)
None
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |