Skip to content

Commit

Permalink
fix(datadog-transport-common): vendor is-network-error
Browse files Browse the repository at this point in the history
  • Loading branch information
theogravity committed Apr 12, 2024
1 parent 7e95fdf commit b819f51
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/pretty-guests-attack.md
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.
1 change: 0 additions & 1 deletion packages/datadog-transport-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
},
"dependencies": {
"@datadog/datadog-api-client": "^1.23.0",
"is-network-error": "^1.1.0",
"retry": "^0.13.1"
},
"devDependencies": {
Expand Down
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;
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);
}
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')));
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.
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"
}
}
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`.
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));
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import retry from 'retry';
import isNetworkError from 'is-network-error';
import isNetworkError from '../is-network-error';

export class AbortError extends Error {
constructor(message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
],
"dependencies": {
"@types/retry": "0.12.2",
"is-network-error": "^1.0.0",
"retry": "^0.13.1"
},
"devDependencies": {
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b819f51

Please sign in to comment.