@@ -3,6 +3,8 @@ import net = require('net');
33import http = require( 'http' ) ;
44import stream = require( 'stream' ) ;
55import test from 'ava' ;
6+ import getStream = require( 'get-stream' ) ;
7+ import is from '@sindresorhus/is' ;
68import got , { RequestError , HTTPError , TimeoutError } from '../source' ;
79import 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