@@ -322,8 +322,41 @@ export class BackendClient extends BaseClient {
322322 expiresAt : Date . now ( ) + ( oauthTokenResponse . expires_in || 0 ) * 1000 ,
323323 } ;
324324 } catch ( e ) {
325- console . log ( e )
326- // pass
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 ;
332+ }
333+
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 } ` ;
349+ }
350+ }
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 ;
359+ }
327360 }
328361
329362 attempts ++ ;
0 commit comments