Skip to content

Commit 7c978a8

Browse files
committed
better error surfacing when getting an access token.
1 parent b0e555c commit 7c978a8

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/sdk/src/server/index.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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++;

packages/sdk/src/shared/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,14 @@ export type ErrorResponse = {
853853
* The error message returned by the API.
854854
*/
855855
error: string;
856+
/**
857+
* The www-authenticate header value, if present in the error response.
858+
*/
859+
wwwAuthenticate?: string;
860+
/**
861+
* Additional error headers that may be relevant for error handling.
862+
*/
863+
headers?: Record<string, string>;
856864
};
857865

858866
/**

0 commit comments

Comments
 (0)