Skip to content

Commit d75f3c6

Browse files
committed
chore: add fromResponse
1 parent e1f7fb9 commit d75f3c6

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/common/atlas/apiClient.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export class ApiClientError extends Error {
3131
this.name = "ApiClientError";
3232
this.response = response;
3333
}
34+
35+
static async fromResponse(response: Response): Promise<ApiClientError> {
36+
try {
37+
const text = await response.text();
38+
return new ApiClientError(`Error calling Atlas API: [${response.status} ${response.statusText}] ${text}`, response);
39+
} catch {
40+
return new ApiClientError(`Error calling Atlas API: ${response.status} ${response.statusText}`, response);
41+
}
42+
}
3443
}
3544

3645
export interface ApiClientOptions {
@@ -62,18 +71,7 @@ export class ApiClient {
6271
private errorMiddleware = (): Middleware => ({
6372
async onResponse({ response }) {
6473
if (!response.ok) {
65-
try {
66-
const text = await response.text();
67-
throw new ApiClientError(
68-
`Error calling Atlas API: [${response.status} ${response.statusText}] ${text}`,
69-
response
70-
);
71-
} catch {
72-
throw new ApiClientError(
73-
`Error calling Atlas API: ${response.status} ${response.statusText}`,
74-
response
75-
);
76-
}
74+
throw await ApiClientError.fromResponse(response);
7775
}
7876
},
7977
});

0 commit comments

Comments
 (0)