@@ -71,6 +71,10 @@ export interface CommonHttpClientOptions {
7171 path : string ;
7272 method : CommonHttpClientFetchRequest [ 'method' ] ;
7373 } ) : void ;
74+ /**
75+ * Determine whether to retry on error.
76+ */
77+ shouldRetryOnError ?: ( error : Error , attemptNumber : number ) => boolean ;
7478}
7579
7680/**
@@ -825,41 +829,51 @@ export class CommonHttpClient {
825829 redirect : redirect ?? 'follow' ,
826830 body : this . getRequestBody ( request )
827831 } ;
828- let fetchResponse : CommonHttpClientFetchResponse ;
829- try {
830- if ( this . options . fetch ) {
831- fetchResponse = await this . options . fetch ( url , fetchRequest ) ;
832- } else {
833- fetchResponse = await this . fetch ( url , fetchRequest ) ;
834- }
835- } catch ( e ) {
836- throw new this . options . errorClass ( url , fetchRequest , undefined , this . options , getErrorMessage ( e ) ) ;
837- }
838- if ( this . options . preprocessFetchResponse ) {
832+ let attemptNumber = 1 ;
833+ for ( ; ; ) {
839834 try {
840- fetchResponse = await this . options . preprocessFetchResponse ( fetchResponse , fetchRequest ) ;
841- } catch ( e ) {
842- throw new this . options . errorClass (
843- url ,
844- fetchRequest ,
845- fetchResponse ,
846- this . options ,
847- `preprocessFetchResponse error: ${ getErrorMessage ( e ) } `
848- ) ;
835+ let fetchResponse : CommonHttpClientFetchResponse ;
836+ try {
837+ if ( this . options . fetch ) {
838+ fetchResponse = await this . options . fetch ( url , fetchRequest ) ;
839+ } else {
840+ fetchResponse = await this . fetch ( url , fetchRequest ) ;
841+ }
842+ } catch ( e ) {
843+ throw new this . options . errorClass ( url , fetchRequest , undefined , this . options , getErrorMessage ( e ) ) ;
844+ }
845+ if ( this . options . preprocessFetchResponse ) {
846+ try {
847+ fetchResponse = await this . options . preprocessFetchResponse ( fetchResponse , fetchRequest ) ;
848+ } catch ( e ) {
849+ throw new this . options . errorClass (
850+ url ,
851+ fetchRequest ,
852+ fetchResponse ,
853+ this . options ,
854+ `preprocessFetchResponse error: ${ getErrorMessage ( e ) } `
855+ ) ;
856+ }
857+ }
858+ if ( ! fetchResponse . ok ) {
859+ throw new this . options . errorClass (
860+ url ,
861+ fetchRequest ,
862+ fetchResponse ,
863+ this . options ,
864+ this . options . formatHttpErrorMessage
865+ ? this . options . formatHttpErrorMessage ( fetchResponse , fetchRequest )
866+ : `HTTP Error ${ request . method } ${ url . toString ( ) } ${ fetchResponse . status } (${ fetchResponse . statusText } )`
867+ ) ;
868+ }
869+ return fetchResponse ;
870+ } catch ( error ) {
871+ if ( ! this . options . shouldRetryOnError ?.( error as Error , attemptNumber ) ) {
872+ throw error ;
873+ }
874+ attemptNumber ++ ;
849875 }
850876 }
851- if ( ! fetchResponse . ok ) {
852- throw new this . options . errorClass (
853- url ,
854- fetchRequest ,
855- fetchResponse ,
856- this . options ,
857- this . options . formatHttpErrorMessage
858- ? this . options . formatHttpErrorMessage ( fetchResponse , fetchRequest )
859- : `HTTP Error ${ request . method } ${ url . toString ( ) } ${ fetchResponse . status } (${ fetchResponse . statusText } )`
860- ) ;
861- }
862- return fetchResponse ;
863877 }
864878
865879 /**
0 commit comments