@@ -298,68 +298,68 @@ export class BackendClient extends BaseClient {
298298 as,
299299 } = this . oauthClient
300300
301- let attempts = 0 ;
302301 const maxAttempts = 2 ;
303302
304303 while ( ! this . oauthAccessToken || this . oauthAccessToken . expiresAt - Date . now ( ) < 1000 ) {
305- if ( attempts > maxAttempts ) {
306- throw new Error ( "ran out of attempts trying to retrieve oauth access token" ) ;
307- }
308- if ( attempts > 0 ) {
309- // Wait for a short duration before retrying to avoid rapid retries
310- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
311- }
304+ for ( let attempts = 0 ; attempts <= maxAttempts ; attempts ++ ) {
305+ if ( attempts > 0 ) {
306+ // Wait for a short duration before retrying to avoid rapid retries
307+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
308+ }
312309
313- const parameters = new URLSearchParams ( ) ;
314- if ( this . scope && this . scope . length > 0 ) {
315- parameters . set ( "scope" , this . scope . join ( " " ) ) ;
316- }
317- try {
318- const response = await oauth . clientCredentialsGrantRequest ( as , client , clientAuth , parameters ) ;
319- const oauthTokenResponse = await oauth . processClientCredentialsResponse ( as , client , response ) ;
320- this . oauthAccessToken = {
321- token : oauthTokenResponse . access_token ,
322- expiresAt : Date . now ( ) + ( oauthTokenResponse . expires_in || 0 ) * 1000 ,
323- } ;
324- } catch ( e ) {
325- // Extract error details from OAuth response
326- let errorMessage = "OAuth token request failed" ;
327- let wwwAuthenticate : string | undefined ;
328- let statusCode : number | undefined ;
329-
330- if ( e instanceof Error ) {
331- errorMessage = e . message ;
310+ const parameters = new URLSearchParams ( ) ;
311+ if ( this . scope && this . scope . length > 0 ) {
312+ parameters . set ( "scope" , this . scope . join ( " " ) ) ;
332313 }
314+ parameters . set ( "project_id" , this . projectId ) ;
315+ parameters . set ( "environment" , this . environment ) ;
333316
334- // Check if the error contains response information
335- if ( e && typeof e === 'object' && 'response' in e ) {
336- const errorResponse = ( e as any ) . response ;
337- if ( errorResponse ) {
338- statusCode = errorResponse . status ;
339- wwwAuthenticate = errorResponse . headers ?. get ?.( 'www-authenticate' ) ||
340- errorResponse . headers ?. [ 'www-authenticate' ] ;
341-
342- // Create more specific error message based on status code
343- if ( statusCode === 401 ) {
344- errorMessage = `OAuth authentication failed (401 Unauthorized)${ wwwAuthenticate ? `: ${ wwwAuthenticate } ` : '' } ` ;
345- } else if ( statusCode === 400 ) {
346- errorMessage = "OAuth request invalid (400 Bad Request) - check client credentials" ;
347- } else if ( statusCode ) {
348- errorMessage = `OAuth request failed with status ${ statusCode } ` ;
317+ try {
318+ const response = await oauth . clientCredentialsGrantRequest ( as , client , clientAuth , parameters ) ;
319+ const oauthTokenResponse = await oauth . processClientCredentialsResponse ( as , client , response ) ;
320+ this . oauthAccessToken = {
321+ token : oauthTokenResponse . access_token ,
322+ expiresAt : Date . now ( ) + ( oauthTokenResponse . expires_in || 0 ) * 1000 ,
323+ } ;
324+ break ; // Successfully got token, exit retry loop
325+ } catch ( e ) {
326+ // Extract error details from OAuth response
327+ let errorMessage = "OAuth token request failed" ;
328+ let wwwAuthenticate : string | undefined ;
329+ let statusCode : number | undefined ;
330+
331+ if ( e instanceof Error ) {
332+ errorMessage = e . message ;
333+ }
334+
335+ // Check if the error contains response information
336+ if ( e && typeof e === 'object' && 'response' in e ) {
337+ const errorResponse = ( e as any ) . response ;
338+ if ( errorResponse ) {
339+ statusCode = errorResponse . status ;
340+ wwwAuthenticate = errorResponse . headers ?. get ?.( 'www-authenticate' ) ||
341+ errorResponse . headers ?. [ 'www-authenticate' ] ;
342+
343+ // Create more specific error message based on status code
344+ if ( statusCode === 401 ) {
345+ errorMessage = `OAuth authentication failed (401 Unauthorized)${ wwwAuthenticate ? `: ${ wwwAuthenticate } ` : '' } ` ;
346+ } else if ( statusCode === 400 ) {
347+ errorMessage = "OAuth request invalid (400 Bad Request) - check client credentials" ;
348+ } else if ( statusCode ) {
349+ errorMessage = `OAuth request failed with status ${ statusCode } ` ;
350+ }
349351 }
350352 }
351- }
352-
353- // If this is the last attempt, throw a detailed error
354- if ( attempts >= maxAttempts ) {
355- const error = new Error ( errorMessage ) ;
356- ( error as any ) . statusCode = statusCode ;
357- ( error as any ) . wwwAuthenticate = wwwAuthenticate ;
358- throw error ;
353+
354+ // If this is the last attempt, throw a detailed error
355+ if ( attempts >= maxAttempts ) {
356+ const error = new Error ( errorMessage ) ;
357+ ( error as any ) . statusCode = statusCode ;
358+ ( error as any ) . wwwAuthenticate = wwwAuthenticate ;
359+ throw error ;
360+ }
359361 }
360362 }
361-
362- attempts ++ ;
363363 }
364364
365365 return this . oauthAccessToken . token ;
0 commit comments