first commit

This commit is contained in:
Stefan Hacker
2026-04-03 09:38:48 +02:00
commit 37ad745546
47450 changed files with 3120798 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
/**
Check if an error is a [Fetch network error](https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions)
@return Returns `true` if the given value is a Fetch network error, otherwise `false`.
@example
```
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());
```
*/
export default function isNetworkError(value: unknown): value is TypeError;
+47
View File
@@ -0,0 +1,47 @@
const objectToString = Object.prototype.toString;
const isError = value => objectToString.call(value) === '[object Error]';
const errorMessages = new Set([
'network error', // Chrome
'NetworkError when attempting to fetch resource.', // Firefox
'The Internet connection appears to be offline.', // Safari 16
'Network request failed', // `cross-fetch`
'fetch failed', // Undici (Node.js)
'terminated', // Undici (Node.js)
' A network error occurred.', // Bun (WebKit)
'Network connection lost', // Cloudflare Workers (fetch)
]);
export default function isNetworkError(error) {
const isValid = error
&& isError(error)
&& error.name === 'TypeError'
&& typeof error.message === 'string';
if (!isValid) {
return false;
}
const {message, stack} = error;
// Safari 17+ has generic message but no stack for network errors
if (message === 'Load failed') {
return stack === undefined
// Sentry adds its own stack trace to the fetch error, so also check for that
|| '__sentry_captured__' in error;
}
// Deno network errors start with specific text
if (message.startsWith('error sending request for url')) {
return true;
}
// Chrome: exact "Failed to fetch" or with hostname: "Failed to fetch (example.com)"
if (message === 'Failed to fetch' || (message.startsWith('Failed to fetch (') && message.endsWith(')'))) {
return true;
}
// Standard network error messages
return errorMessages.has(message);
}
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+43
View File
@@ -0,0 +1,43 @@
{
"name": "is-network-error",
"version": "1.3.1",
"description": "Check if a value is a Fetch network error",
"license": "MIT",
"repository": "sindresorhus/is-network-error",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=16"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"network",
"error",
"fetch",
"whatwg",
"detect",
"check",
"typeerror"
],
"devDependencies": {
"ava": "^5.3.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
+44
View File
@@ -0,0 +1,44 @@
# is-network-error
> Check if a value is a [Fetch network error](https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions)
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](https://github.com/whatwg/fetch/issues/526) and differ among implementations. This package handles the differences across Node.js, Bun, Deno, and browsers.
For instance, [`p-retry`](https://github.com/sindresorhus/p-retry) uses this package to retry on network errors.
## Install
```sh
npm install is-network-error
```
## Usage
```js
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`.