@@ -62,8 +62,7 @@ export interface ReqOptions {
6262 query ?: Record < string , any > ;
6363 method : 'GET' | 'POST' | 'PUT' | 'DELETE' ;
6464 body ?: any ;
65- attempt ?: number ;
66- maxAttempts ?: number ;
65+ retries ?: number ;
6766}
6867
6968const delay = ( ms : number ) =>
@@ -87,44 +86,47 @@ export class KbnClientRequester {
8786 async request < T > ( options : ReqOptions ) : Promise < T > {
8887 const url = Url . resolve ( this . pickUrl ( ) , options . path ) ;
8988 const description = options . description || `${ options . method } ${ url } ` ;
90- const attempt = options . attempt === undefined ? 1 : options . attempt ;
91- const maxAttempts =
92- options . maxAttempts === undefined ? DEFAULT_MAX_ATTEMPTS : options . maxAttempts ;
93-
94- try {
95- const response = await Axios . request < T > ( {
96- method : options . method ,
97- url,
98- data : options . body ,
99- params : options . query ,
100- headers : {
101- 'kbn-xsrf' : 'kbn-client' ,
102- } ,
103- } ) ;
104-
105- return response . data ;
106- } catch ( error ) {
107- let retryErrorMsg : string | undefined ;
108- if ( isAxiosRequestError ( error ) ) {
109- retryErrorMsg = `[${ description } ] request failed (attempt=${ attempt } )` ;
110- } else if ( isConcliftOnGetError ( error ) ) {
111- retryErrorMsg = `Conflict on GET (path=${ options . path } , attempt=${ attempt } )` ;
112- }
89+ let attempt = 0 ;
90+ const maxAttempts = options . retries ?? DEFAULT_MAX_ATTEMPTS ;
91+
92+ while ( true ) {
93+ attempt += 1 ;
94+
95+ try {
96+ const response = await Axios . request < T > ( {
97+ method : options . method ,
98+ url,
99+ data : options . body ,
100+ params : options . query ,
101+ headers : {
102+ 'kbn-xsrf' : 'kbn-client' ,
103+ } ,
104+ } ) ;
105+
106+ return response . data ;
107+ } catch ( error ) {
108+ const conflictOnGet = isConcliftOnGetError ( error ) ;
109+ const requestedRetries = options . retries !== undefined ;
110+ const failedToGetResponse = isAxiosRequestError ( error ) ;
111+
112+ let errorMessage ;
113+ if ( conflictOnGet ) {
114+ errorMessage = `Conflict on GET (path=${ options . path } , attempt=${ attempt } /${ maxAttempts } )` ;
115+ this . log . error ( errorMessage ) ;
116+ } else if ( requestedRetries || failedToGetResponse ) {
117+ errorMessage = `[${ description } ] request failed (attempt=${ attempt } /${ maxAttempts } )` ;
118+ this . log . error ( errorMessage ) ;
119+ } else {
120+ throw error ;
121+ }
113122
114- if ( retryErrorMsg ) {
115123 if ( attempt < maxAttempts ) {
116- this . log . error ( retryErrorMsg ) ;
117124 await delay ( 1000 * attempt ) ;
118- return await this . request < T > ( {
119- ...options ,
120- attempt : attempt + 1 ,
121- } ) ;
125+ continue ;
122126 }
123127
124- throw new Error ( retryErrorMsg + ' and ran out of retries' ) ;
128+ throw new Error ( ` ${ errorMessage } -- and ran out of retries` ) ;
125129 }
126-
127- throw error ;
128130 }
129131 }
130132}
0 commit comments