Fix a2amcp login error reporting
All checks were successful
build / test (push) Successful in 20s
publish / npm (push) Successful in 22s

This commit is contained in:
2026-05-31 21:03:15 -03:00
parent 2814112ab3
commit f0b2d9ead8
5 changed files with 87 additions and 16 deletions

View File

@@ -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 {};
}
}