Skip to content

Commit f4613f4

Browse files
committed
fix: improve api error messages
1 parent de2c604 commit f4613f4

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/common/atlas/apiClientError.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1-
export class ApiClientError extends Error {
2-
response?: Response;
1+
import { ApiError } from "./openapi.js";
32

4-
constructor(message: string, response: Response | undefined = undefined) {
3+
export class ApiClientError extends Error {
4+
private constructor(
5+
message: string,
6+
public readonly response?: Response,
7+
public readonly body?: ApiError
8+
) {
59
super(message);
610
this.name = "ApiClientError";
7-
this.response = response;
811
}
912

1013
static async fromResponse(
1114
response: Response,
1215
message: string = `error calling Atlas API`
1316
): Promise<ApiClientError> {
17+
let text: string = "";
18+
let body: ApiError | undefined = undefined;
1419
try {
15-
const text = await response.text();
16-
return new ApiClientError(`${message}: [${response.status} ${response.statusText}] ${text}`, response);
20+
body = (await response.json()) as ApiError;
21+
text = body.reason || "unknown error";
22+
if (body.detail && body.detail.length > 0) {
23+
text = `${text}; ${body.detail}`;
24+
}
1725
} catch {
18-
return new ApiClientError(`${message}: ${response.status} ${response.statusText}`, response);
26+
try {
27+
text = await response.text();
28+
} catch {
29+
text = "";
30+
}
1931
}
32+
33+
if (text.length > 0) {
34+
text = `${message}: [${response.status} ${response.statusText}] ${text.trim()}`;
35+
} else {
36+
text = `${message}: ${response.status} ${response.statusText}`;
37+
}
38+
39+
return new ApiClientError(text, response, body);
2040
}
2141
}

0 commit comments

Comments
 (0)