Fix a2amcp login error reporting
This commit is contained in:
32
src/api.ts
32
src/api.ts
@@ -24,8 +24,11 @@ export class ControlPlaneClient {
|
||||
}
|
||||
|
||||
private headers(): Record<string, string> {
|
||||
const h: Record<string, string> = { "content-type": "application/json" };
|
||||
if (this.token) h["authorization"] = `bearer ${this.token}`;
|
||||
const h: Record<string, string> = {
|
||||
accept: "application/json",
|
||||
"content-type": "application/json",
|
||||
};
|
||||
if (this.token) h["authorization"] = `Bearer ${this.token}`;
|
||||
return h;
|
||||
}
|
||||
|
||||
@@ -35,18 +38,12 @@ export class ControlPlaneClient {
|
||||
headers: this.headers(),
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
const text = await resp.text();
|
||||
if (!resp.ok) {
|
||||
let detail: string;
|
||||
try {
|
||||
const j: any = await resp.json();
|
||||
detail = j?.detail ?? JSON.stringify(j);
|
||||
} catch {
|
||||
detail = await resp.text();
|
||||
}
|
||||
throw new ApiError(resp.status, detail);
|
||||
throw new ApiError(resp.status, errorDetail(text, resp.statusText));
|
||||
}
|
||||
if (resp.status === 204) return undefined as T;
|
||||
return (await resp.json()) as T;
|
||||
return (text ? JSON.parse(text) : undefined) as T;
|
||||
}
|
||||
|
||||
me() {
|
||||
@@ -61,3 +58,16 @@ export class ControlPlaneClient {
|
||||
return this.request<AgentRow>("GET", `/v1/agents/${encodeURIComponent(name)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function errorDetail(text: string, fallback: string): string {
|
||||
if (!text.trim()) return fallback;
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
const detail = parsed?.detail;
|
||||
if (typeof detail === "string") return detail;
|
||||
if (detail !== undefined) return JSON.stringify(detail);
|
||||
return JSON.stringify(parsed);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user