Skip to content

Commit 49c16ee

Browse files
authoredNov 2, 2020
Do not throw on custom stack traces (#1491)
1 parent 2b8ed1f commit 49c16ee

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
 

‎source/core/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ export class RequestError extends Error {
12181218
this.timings = this.request?.timings;
12191219

12201220
// Recover the original stacktrace
1221-
if (!is.undefined(error.stack)) {
1221+
if (is.string(error.stack) && is.string(this.stack)) {
12221222
const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
12231223
const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
12241224
const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message!) + error.message!.length).split('\n').reverse();

‎test/error.ts

+69
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import net = require('net');
33
import http = require('http');
44
import stream = require('stream');
55
import test from 'ava';
6+
import getStream = require('get-stream');
7+
import is from '@sindresorhus/is';
68
import got, {RequestError, HTTPError, TimeoutError} from '../source';
79
import withServer from './helpers/with-server';
810

@@ -261,3 +263,70 @@ test.skip('the old stacktrace is recovered', async t => {
261263
// the second `at get` points to the real cause.
262264
t.not(error.stack!.indexOf('at get'), error.stack!.lastIndexOf('at get'));
263265
});
266+
267+
test.serial('custom stack trace', withServer, async (t, _server, got) => {
268+
const ErrorCaptureStackTrace = Error.captureStackTrace;
269+
270+
const enable = () => {
271+
Error.captureStackTrace = (target: {stack: any}) => {
272+
target.stack = [
273+
'line 1',
274+
'line 2'
275+
];
276+
};
277+
};
278+
279+
const disable = () => {
280+
Error.captureStackTrace = ErrorCaptureStackTrace;
281+
};
282+
283+
// Node.js default behavior
284+
{
285+
const stream = got.stream('');
286+
stream.destroy(new Error('oh no'));
287+
288+
const caught = await t.throwsAsync(getStream(stream));
289+
t.is(is(caught.stack), 'string');
290+
}
291+
292+
// Passing a custom error
293+
{
294+
enable();
295+
const error = new Error('oh no');
296+
disable();
297+
298+
const stream = got.stream('');
299+
stream.destroy(error);
300+
301+
const caught = await t.throwsAsync(getStream(stream));
302+
t.is(is(caught.stack), 'string');
303+
}
304+
305+
// Custom global behavior
306+
{
307+
enable();
308+
const error = new Error('oh no');
309+
310+
const stream = got.stream('');
311+
stream.destroy(error);
312+
313+
const caught = await t.throwsAsync(getStream(stream));
314+
t.is(is(caught.stack), 'Array');
315+
316+
disable();
317+
}
318+
319+
// Passing a default error that needs some processing
320+
{
321+
const error = new Error('oh no');
322+
enable();
323+
324+
const stream = got.stream('');
325+
stream.destroy(error);
326+
327+
const caught = await t.throwsAsync(getStream(stream));
328+
t.is(is(caught.stack), 'Array');
329+
330+
disable();
331+
}
332+
});

0 commit comments

Comments
 (0)
Please sign in to comment.