@@ -33,7 +33,7 @@ import { ServerMockedEndpoint } from "./mocked-endpoint";
3333import { createComboServer } from "./http-combo-server" ;
3434import { filter } from "../util/promise" ;
3535import { Mutable } from "../util/type-utils" ;
36- import { isErrorLike } from "../util/error" ;
36+ import { ErrorLike , isErrorLike } from "../util/error" ;
3737import { makePropertyWritable } from "../util/util" ;
3838
3939import {
@@ -480,12 +480,18 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
480480 } ) ;
481481 }
482482
483- private async announceAbortAsync ( request : OngoingRequest ) {
483+ private async announceAbortAsync ( request : OngoingRequest , abortError ?: ErrorLike ) {
484484 setImmediate ( ( ) => {
485485 const req = buildInitiatedRequest ( request ) ;
486486 this . eventEmitter . emit ( 'abort' , Object . assign ( req , {
487487 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
489495 } ) ) ;
490496 } ) ;
491497 }
@@ -582,18 +588,18 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
582588 if ( this . debug ) console . log ( `Handling request for ${ rawRequest . url } ` ) ;
583589
584590 let result : 'responded' | 'aborted' | null = null ;
585- const abort = ( ) => {
591+ const abort = ( error ?: Error ) => {
586592 if ( result === null ) {
587593 result = 'aborted' ;
588594 request . timingEvents . abortedTimestamp = now ( ) ;
589- this . announceAbortAsync ( request ) ;
595+ this . announceAbortAsync ( request , error ) ;
590596 }
591597 }
592598 request . once ( 'aborted' , abort ) ;
593599 // In Node 16+ we don't get an abort event in many cases, just closes, but we know
594600 // it's aborted because the response is closed with no other result being set.
595601 rawResponse . once ( 'close' , ( ) => setImmediate ( abort ) ) ;
596- request . once ( 'error' , ( ) => setImmediate ( abort ) ) ;
602+ request . once ( 'error' , ( error ) => setImmediate ( ( ) => abort ( error ) ) ) ;
597603
598604 this . announceInitialRequestAsync ( request ) ;
599605
@@ -606,7 +612,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
606612 response . id = request . id ;
607613 response . on ( 'error' , ( error ) => {
608614 console . log ( 'Response error:' , this . debug ? error : error . message ) ;
609- abort ( ) ;
615+ abort ( error ) ;
610616 } ) ;
611617
612618 try {
@@ -631,7 +637,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
631637 result = result || 'responded' ;
632638 } catch ( e ) {
633639 if ( e instanceof AbortError ) {
634- abort ( ) ;
640+ abort ( e ) ;
635641
636642 if ( this . debug ) {
637643 console . error ( "Failed to handle request due to abort:" , e ) ;
@@ -655,7 +661,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
655661 response . end ( ( isErrorLike ( e ) && e . toString ( ) ) || e ) ;
656662 result = result || 'responded' ;
657663 } catch ( e ) {
658- abort ( ) ;
664+ abort ( e as Error ) ;
659665 }
660666 }
661667 }
0 commit comments