@@ -33,7 +33,7 @@ import { ServerMockedEndpoint } from "./mocked-endpoint";
33
33
import { createComboServer } from "./http-combo-server" ;
34
34
import { filter } from "../util/promise" ;
35
35
import { Mutable } from "../util/type-utils" ;
36
- import { isErrorLike } from "../util/error" ;
36
+ import { ErrorLike , isErrorLike } from "../util/error" ;
37
37
import { makePropertyWritable } from "../util/util" ;
38
38
39
39
import {
@@ -480,12 +480,18 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
480
480
} ) ;
481
481
}
482
482
483
- private async announceAbortAsync ( request : OngoingRequest ) {
483
+ private async announceAbortAsync ( request : OngoingRequest , abortError ?: ErrorLike ) {
484
484
setImmediate ( ( ) => {
485
485
const req = buildInitiatedRequest ( request ) ;
486
486
this . eventEmitter . emit ( 'abort' , Object . assign ( req , {
487
487
timingEvents : _ . clone ( req . timingEvents ) ,
488
- tags : _ . clone ( req . tags )
488
+ tags : _ . clone ( req . tags ) ,
489
+ error : abortError ? {
490
+ name : abortError . name ,
491
+ code : abortError . code ,
492
+ message : abortError . message ,
493
+ stack : abortError . stack
494
+ } : undefined
489
495
} ) ) ;
490
496
} ) ;
491
497
}
@@ -582,18 +588,18 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
582
588
if ( this . debug ) console . log ( `Handling request for ${ rawRequest . url } ` ) ;
583
589
584
590
let result : 'responded' | 'aborted' | null = null ;
585
- const abort = ( ) => {
591
+ const abort = ( error ?: Error ) => {
586
592
if ( result === null ) {
587
593
result = 'aborted' ;
588
594
request . timingEvents . abortedTimestamp = now ( ) ;
589
- this . announceAbortAsync ( request ) ;
595
+ this . announceAbortAsync ( request , error ) ;
590
596
}
591
597
}
592
598
request . once ( 'aborted' , abort ) ;
593
599
// In Node 16+ we don't get an abort event in many cases, just closes, but we know
594
600
// it's aborted because the response is closed with no other result being set.
595
601
rawResponse . once ( 'close' , ( ) => setImmediate ( abort ) ) ;
596
- request . once ( 'error' , ( ) => setImmediate ( abort ) ) ;
602
+ request . once ( 'error' , ( error ) => setImmediate ( ( ) => abort ( error ) ) ) ;
597
603
598
604
this . announceInitialRequestAsync ( request ) ;
599
605
@@ -606,7 +612,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
606
612
response . id = request . id ;
607
613
response . on ( 'error' , ( error ) => {
608
614
console . log ( 'Response error:' , this . debug ? error : error . message ) ;
609
- abort ( ) ;
615
+ abort ( error ) ;
610
616
} ) ;
611
617
612
618
try {
@@ -631,7 +637,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
631
637
result = result || 'responded' ;
632
638
} catch ( e ) {
633
639
if ( e instanceof AbortError ) {
634
- abort ( ) ;
640
+ abort ( e ) ;
635
641
636
642
if ( this . debug ) {
637
643
console . error ( "Failed to handle request due to abort:" , e ) ;
@@ -655,7 +661,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
655
661
response . end ( ( isErrorLike ( e ) && e . toString ( ) ) || e ) ;
656
662
result = result || 'responded' ;
657
663
} catch ( e ) {
658
- abort ( ) ;
664
+ abort ( e as Error ) ;
659
665
}
660
666
}
661
667
}
0 commit comments