Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add original error property #2327

Merged
merged 4 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions documentation/8-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
katsanva marked this conversation as resolved.
Show resolved Hide resolved

### `RequestError`

Expand Down Expand Up @@ -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`

Expand All @@ -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`**

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion source/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class RequestError<T = unknown> extends Error {
readonly timings?: Timings;

constructor(message: string, error: Partial<Error & {code?: string}>, self: Request | Options) {
super(message);
super(message, {cause: error});
Error.captureStackTrace(this, this.constructor);

this.name = 'RequestError';
Expand Down
17 changes: 17 additions & 0 deletions test/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<RequestError>(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<RequestError>(got('https://github.com', {retry: {limit: 0}, timeout: {read: 1}, agent: {https: new SocksProxyAgent('socks://your-name%40gmail.com:abcdef12345124@br41.nordvpn.com')}}));
katsanva marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down