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

Do not throw on custom stack traces #1491

Merged
merged 4 commits into from Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/core/index.ts
Expand Up @@ -1222,7 +1222,7 @@ export class RequestError extends Error {
this.timings = this.request?.timings;

// Recover the original stacktrace
if (!is.undefined(error.stack)) {
if (is.string(error.stack) && is.string(this.stack)) {
const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message!) + error.message!.length).split('\n').reverse();
Expand Down
69 changes: 69 additions & 0 deletions test/error.ts
Expand Up @@ -3,6 +3,8 @@ import net = require('net');
import http = require('http');
import stream = require('stream');
import test from 'ava';
import getStream = require('get-stream');
import is from '@sindresorhus/is';
import got, {RequestError, HTTPError, TimeoutError} from '../source';
import withServer from './helpers/with-server';

Expand Down Expand Up @@ -261,3 +263,70 @@ test.skip('the old stacktrace is recovered', async t => {
// the second `at get` points to the real cause.
t.not(error.stack!.indexOf('at get'), error.stack!.lastIndexOf('at get'));
});

test.serial('custom stack trace', withServer, async (t, _server, got) => {
const ErrorCaptureStackTrace = Error.captureStackTrace;

const enable = () => {
Error.captureStackTrace = (target: {stack: any}) => {
target.stack = [
'line 1',
'line 2'
];
};
};

const disable = () => {
Error.captureStackTrace = ErrorCaptureStackTrace;
};

// Node.js default behavior
{
const stream = got.stream('');
stream.destroy(new Error('oh no'));

const caught = await t.throwsAsync(getStream(stream));
t.is(is(caught.stack), 'string');
}

// Passing a custom error
{
enable();
const error = new Error('oh no');
disable();

const stream = got.stream('');
stream.destroy(error);

const caught = await t.throwsAsync(getStream(stream));
t.is(is(caught.stack), 'string');
}

// Custom global behavior
{
enable();
const error = new Error('oh no');

const stream = got.stream('');
stream.destroy(error);

const caught = await t.throwsAsync(getStream(stream));
t.is(is(caught.stack), 'Array');

disable();
}

// Passing a default error that needs some processing
{
const error = new Error('oh no');
enable();

const stream = got.stream('');
stream.destroy(error);

const caught = await t.throwsAsync(getStream(stream));
t.is(is(caught.stack), 'Array');

disable();
}
});