Fix a2amcp login error reporting
This commit is contained in:
15
src/oauth.ts
15
src/oauth.ts
@@ -208,9 +208,10 @@ async function exchangeAuthorizationCode(opts: {
|
||||
}
|
||||
|
||||
async function parseTokenResponse(resp: Response): Promise<TokenResponse> {
|
||||
const data = (await resp.json().catch(() => ({}))) as TokenResponse;
|
||||
const text = await resp.text();
|
||||
const data = parseJsonObject(text) as TokenResponse;
|
||||
if (!resp.ok || !data.access_token) {
|
||||
const message = data.error_description || data.error || resp.statusText;
|
||||
const message = data.error_description || data.error || text.trim() || resp.statusText;
|
||||
throw new Error(`Keycloak token exchange failed: ${message}`);
|
||||
}
|
||||
return data;
|
||||
@@ -336,3 +337,13 @@ function expiresAt(expiresIn: number | undefined): number | undefined {
|
||||
function nowSeconds(): number {
|
||||
return Math.floor(Date.now() / 1000);
|
||||
}
|
||||
|
||||
function parseJsonObject(text: string): Record<string, unknown> {
|
||||
if (!text.trim()) return {};
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
return parsed && typeof parsed === "object" ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user