-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datadog-transport-common): vendor is-network-error
- Loading branch information
1 parent
7e95fdf
commit b819f51
Showing
12 changed files
with
172 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"datadog-transport-common": patch | ||
"electron-log-transport-datadog": patch | ||
"pino-datadog-transport": patch | ||
--- | ||
|
||
Fix ERR_REQUIRE_ESM import error caused by is-network-error package by vendoring it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/datadog-transport-common/src/vendor/is-network-error/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
33 changes: 33 additions & 0 deletions
33
packages/datadog-transport-common/src/vendor/is-network-error/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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) | ||
]); | ||
|
||
export default 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); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/datadog-transport-common/src/vendor/is-network-error/index.test-d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {expectType} from 'tsd'; | ||
import isNetworkError from './index.js'; | ||
|
||
expectType<boolean>(isNetworkError(new TypeError('Failed to fetch'))); |
9 changes: 9 additions & 0 deletions
9
packages/datadog-transport-common/src/vendor/is-network-error/license
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <[email protected]> (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 changes: 43 additions & 0 deletions
43
packages/datadog-transport-common/src/vendor/is-network-error/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "is-network-error", | ||
"version": "1.1.0", | ||
"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": "[email protected]", | ||
"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" | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/datadog-transport-common/src/vendor/is-network-error/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 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. | ||
|
||
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): boolean` | ||
|
||
Returns `true` if the given value is a Fetch network error, otherwise `false`. |
7 changes: 7 additions & 0 deletions
7
packages/datadog-transport-common/src/vendor/is-network-error/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import test from 'ava'; | ||
import isNetworkError from './index.js'; | ||
|
||
test('main', async t => { | ||
const error = await t.throwsAsync(fetch('https://asdfhsdflasudgfadsjyhgfjashgfaskjh.com')); | ||
t.true(isNetworkError(error)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.