Skip to content

Commit 4cbd01d

Browse files
katsanvasindresorhus
andauthoredFeb 3, 2024
Add cause property with the original error to RequestError (#2327)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent f10e151 commit 4cbd01d

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed
 

‎documentation/8-errors.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ All Got errors contain various metadata, such as:
1919

2020
Read the article [here](async-stack-traces.md).
2121

22-
**Note:**
22+
> [!NOTE]
2323
> - The error codes may differ when the root error has a `code` property set.
24+
> - 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.
2425
2526
### `RequestError`
2627

@@ -62,8 +63,8 @@ A request is successful when the status code of the final request is `2xx` or `3
6263

6364
When [following redirects](2-options.md#followredirect), a request is successful **only** when the status code of the final request is `2xx`.
6465

65-
**Note:**
66-
> - `304` responses are always considered successful.
66+
> [!NOTE]
67+
> `304` responses are always considered successful.
6768
6869
### `MaxRedirectsError`
6970

@@ -73,8 +74,8 @@ When the server redirects you more than ten times. Includes a `response` propert
7374

7475
### `UnsupportedProtocolError`
7576

76-
**Note:**
77-
> - This error is not public.
77+
> [!NOTE]
78+
> This error is not public.
7879
7980
**Code: `ERR_UNSUPPORTED_PROTOCOL`**
8081

‎source/core/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class RequestError<T = unknown> extends Error {
2727
readonly timings?: Timings;
2828

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

3333
this.name = 'RequestError';

‎test/error.ts

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import net from 'node:net';
44
import http from 'node:http';
55
import stream from 'node:stream';
66
import {pipeline as streamPipeline} from 'node:stream/promises';
7+
import {Agent} from 'node:https';
78
import test from 'ava';
89
import getStream from 'get-stream';
910
import is from '@sindresorhus/is';
@@ -359,6 +360,28 @@ test.skip('the old stacktrace is recovered', async t => {
359360
t.not(error?.stack!.indexOf('at get'), error?.stack!.lastIndexOf('at get'));
360361
});
361362

363+
test('should wrap got cause', async t => {
364+
const error = await t.throwsAsync<RequestError>(got('https://github.com', {retry: {limit: 0}, timeout: {request: 1}}));
365+
const cause = error?.cause as TimeoutError;
366+
t.is(error?.code, cause.code);
367+
t.is(error?.message, cause.message);
368+
});
369+
370+
test('should wrap non-got cause', async t => {
371+
class SocksProxyAgent extends Agent {
372+
createConnection() {
373+
throw new SocksClientError('oh no');
374+
}
375+
}
376+
class SocksClientError extends Error {}
377+
const error = await t.throwsAsync<RequestError>(got('https://github.com', {retry: {limit: 0}, timeout: {read: 1}, agent: {https: new SocksProxyAgent()}}));
378+
const cause = error?.cause as Error;
379+
t.is(error?.code, 'ERR_GOT_REQUEST_ERROR');
380+
t.is(error?.message, cause.message);
381+
t.is(error?.message, 'oh no');
382+
t.assert(cause instanceof SocksClientError);
383+
});
384+
362385
test.serial('custom stack trace', withServer, async (t, _server, got) => {
363386
// eslint-disable-next-line @typescript-eslint/naming-convention
364387
const ErrorCaptureStackTrace = Error.captureStackTrace;

0 commit comments

Comments
 (0)
Please sign in to comment.