forked from sindresorhus/is-network-error
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
37 lines (29 loc) · 977 Bytes
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const objectToString = Object.prototype.toString;
const isError = value => objectToString.call(value) === '[object Error]';
const errorMessages = new Set([
'network error', // Chrome
'Failed to fetch', // Chrome
'NetworkError when attempting to fetch resource.', // Firefox
'The Internet connection appears to be offline.', // Safari 16
'Load failed', // Safari 17+
'Network request failed', // `cross-fetch`
'fetch failed', // Undici (Node.js)
'terminated', // Undici (Node.js)
]);
function isNetworkError(error) {
const isValid = error
&& isError(error)
&& error.name === 'TypeError'
&& typeof error.message === 'string';
if (!isValid) {
return false;
}
// We do an extra check for Safari 17+ as it has a very generic error message.
// Network errors in Safari have no stack.
if (error.message === 'Load failed') {
return error.stack === undefined;
}
return errorMessages.has(error.message);
}
module.exports = isNetworkError;