From a26165abbaa7ddbf47b4745396c2342277e73c1a Mon Sep 17 00:00:00 2001 From: Ivan Motiienko <1161259+katsanva@users.noreply.github.com> Date: Tue, 30 Jan 2024 00:06:37 +0100 Subject: [PATCH 1/4] feat: add original error property --- documentation/8-errors.md | 11 ++++++----- package.json | 2 ++ source/core/errors.ts | 2 +- test/error.ts | 17 +++++++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/documentation/8-errors.md b/documentation/8-errors.md index 2cb97aa7d..38cb8a0b4 100644 --- a/documentation/8-errors.md +++ b/documentation/8-errors.md @@ -19,8 +19,9 @@ All Got errors contain various metadata, such as: Read the article [here](async-stack-traces.md). -**Note:** +> [!NOTE] > - The error codes may differ when the root error has a `code` property set. +> - Root error will be propagated as is via [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) property. ### `RequestError` @@ -62,8 +63,8 @@ A request is successful when the status code of the final request is `2xx` or `3 When [following redirects](2-options.md#followredirect), a request is successful **only** when the status code of the final request is `2xx`. -**Note:** -> - `304` responses are always considered successful. +> [!NOTE] +> `304` responses are always considered successful. ### `MaxRedirectsError` @@ -73,8 +74,8 @@ When the server redirects you more than ten times. Includes a `response` propert ### `UnsupportedProtocolError` -**Note:** -> - This error is not public. +> [!NOTE] +> This error is not public. **Code: `ERR_UNSUPPORTED_PROTOCOL`** diff --git a/package.json b/package.json index f635a9e21..f9155b49b 100644 --- a/package.json +++ b/package.json @@ -95,6 +95,8 @@ "request": "^2.88.2", "sinon": "^17.0.1", "slow-stream": "0.0.4", + "socks": "^2.7.1", + "socks-proxy-agent": "^8.0.2", "tempy": "^3.1.0", "then-busboy": "^5.2.1", "tough-cookie": "^4.1.3", diff --git a/source/core/errors.ts b/source/core/errors.ts index 07e50705f..cda0f7cc2 100644 --- a/source/core/errors.ts +++ b/source/core/errors.ts @@ -27,7 +27,7 @@ export class RequestError extends Error { readonly timings?: Timings; constructor(message: string, error: Partial, self: Request | Options) { - super(message); + super(message, {cause: error}); Error.captureStackTrace(this, this.constructor); this.name = 'RequestError'; diff --git a/test/error.ts b/test/error.ts index d915de231..d1192dd48 100644 --- a/test/error.ts +++ b/test/error.ts @@ -7,6 +7,8 @@ import {pipeline as streamPipeline} from 'node:stream/promises'; import test from 'ava'; import getStream from 'get-stream'; import is from '@sindresorhus/is'; +import {SocksProxyAgent} from 'socks-proxy-agent'; +import {SocksClientError} from 'socks'; import got, {RequestError, HTTPError, TimeoutError} from '../source/index.js'; import type Request from '../source/core/index.js'; import withServer from './helpers/with-server.js'; @@ -359,6 +361,21 @@ test.skip('the old stacktrace is recovered', async t => { t.not(error?.stack!.indexOf('at get'), error?.stack!.lastIndexOf('at get')); }); +test('should wrap got cause', async t => { + const error = await t.throwsAsync(got('https://github.com', {retry: {limit: 0}, timeout: {request: 1}})); + const cause = error?.cause as TimeoutError; + t.is(error?.code, cause.code); + t.is(error?.message, cause.message); +}); + +test('should wrap non-got cause', async t => { + const error = await t.throwsAsync(got('https://github.com', {retry: {limit: 0}, timeout: {read: 1}, agent: {https: new SocksProxyAgent('socks://your-name%40gmail.com:abcdef12345124@br41.nordvpn.com')}})); + const cause = error?.cause as Error; + t.is(error?.code, 'ERR_GOT_REQUEST_ERROR'); + t.is(error?.message, cause.message); + t.assert(cause instanceof SocksClientError); +}); + test.serial('custom stack trace', withServer, async (t, _server, got) => { // eslint-disable-next-line @typescript-eslint/naming-convention const ErrorCaptureStackTrace = Error.captureStackTrace; From 6578727b881e91f90893676bf8f428cdf08ba4be Mon Sep 17 00:00:00 2001 From: Ivan Motiienko <1161259+katsanva@users.noreply.github.com> Date: Tue, 30 Jan 2024 21:02:49 +0100 Subject: [PATCH 2/4] chore: removed unneeded modules --- package.json | 2 -- test/error.ts | 10 ++++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index f9155b49b..f635a9e21 100644 --- a/package.json +++ b/package.json @@ -95,8 +95,6 @@ "request": "^2.88.2", "sinon": "^17.0.1", "slow-stream": "0.0.4", - "socks": "^2.7.1", - "socks-proxy-agent": "^8.0.2", "tempy": "^3.1.0", "then-busboy": "^5.2.1", "tough-cookie": "^4.1.3", diff --git a/test/error.ts b/test/error.ts index d1192dd48..dc8b02d8d 100644 --- a/test/error.ts +++ b/test/error.ts @@ -4,11 +4,10 @@ import net from 'node:net'; import http from 'node:http'; import stream from 'node:stream'; import {pipeline as streamPipeline} from 'node:stream/promises'; +import {Agent} from 'node:https'; import test from 'ava'; import getStream from 'get-stream'; import is from '@sindresorhus/is'; -import {SocksProxyAgent} from 'socks-proxy-agent'; -import {SocksClientError} from 'socks'; import got, {RequestError, HTTPError, TimeoutError} from '../source/index.js'; import type Request from '../source/core/index.js'; import withServer from './helpers/with-server.js'; @@ -369,10 +368,17 @@ test('should wrap got cause', async t => { }); test('should wrap non-got cause', async t => { + class SocksProxyAgent extends Agent { + createConnection() { + throw new SocksClientError('oh no'); + } + } + class SocksClientError extends Error {} const error = await t.throwsAsync(got('https://github.com', {retry: {limit: 0}, timeout: {read: 1}, agent: {https: new SocksProxyAgent('socks://your-name%40gmail.com:abcdef12345124@br41.nordvpn.com')}})); const cause = error?.cause as Error; t.is(error?.code, 'ERR_GOT_REQUEST_ERROR'); t.is(error?.message, cause.message); + t.is(error?.message, 'oh no'); t.assert(cause instanceof SocksClientError); }); From 21ed04c10f0e4cd8dae1c0c56e5aabd0179ac988 Mon Sep 17 00:00:00 2001 From: Ivan Motiienko <1161259+katsanva@users.noreply.github.com> Date: Tue, 30 Jan 2024 21:05:15 +0100 Subject: [PATCH 3/4] Update documentation/8-errors.md Co-authored-by: Sindre Sorhus --- documentation/8-errors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/8-errors.md b/documentation/8-errors.md index 38cb8a0b4..ede9f8d6a 100644 --- a/documentation/8-errors.md +++ b/documentation/8-errors.md @@ -21,7 +21,7 @@ Read the article [here](async-stack-traces.md). > [!NOTE] > - The error codes may differ when the root error has a `code` property set. -> - Root error will be propagated as is via [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) property. +> - The root error will be propagated as is via the [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) property. ### `RequestError` From b20023d50c96f1f107829f9d54c0acdab21609fc Mon Sep 17 00:00:00 2001 From: Ivan Motiienko <1161259+katsanva@users.noreply.github.com> Date: Tue, 30 Jan 2024 21:07:05 +0100 Subject: [PATCH 4/4] chore: typo --- test/error.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/error.ts b/test/error.ts index dc8b02d8d..273e08308 100644 --- a/test/error.ts +++ b/test/error.ts @@ -374,7 +374,7 @@ test('should wrap non-got cause', async t => { } } class SocksClientError extends Error {} - const error = await t.throwsAsync(got('https://github.com', {retry: {limit: 0}, timeout: {read: 1}, agent: {https: new SocksProxyAgent('socks://your-name%40gmail.com:abcdef12345124@br41.nordvpn.com')}})); + const error = await t.throwsAsync(got('https://github.com', {retry: {limit: 0}, timeout: {read: 1}, agent: {https: new SocksProxyAgent()}})); const cause = error?.cause as Error; t.is(error?.code, 'ERR_GOT_REQUEST_ERROR'); t.is(error?.message, cause.message);