Skip to content

Commit fb6889f

Browse files
committed
fix: error handling
1 parent 878408e commit fb6889f

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/common/atlas/apiClient.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ export class ApiClientError extends Error {
3232
this.response = response;
3333
}
3434

35-
static async fromResponse(response: Response): Promise<ApiClientError> {
35+
static async fromResponse(response: Response, message?: string): Promise<ApiClientError> {
36+
message ||= `error calling Atlas API`;
3637
try {
3738
const text = await response.text();
3839
return new ApiClientError(
39-
`Error calling Atlas API: [${response.status} ${response.statusText}] ${text}`,
40+
`${message}: [${response.status} ${response.statusText}] ${text}`,
4041
response
4142
);
4243
} catch {
43-
return new ApiClientError(`Error calling Atlas API: ${response.status} ${response.statusText}`, response);
44+
return new ApiClientError(`${message}: ${response.status} ${response.statusText}`, response);
4445
}
4546
}
4647
}
@@ -116,7 +117,7 @@ export class ApiClient {
116117
});
117118

118119
if (!response.ok) {
119-
throw new ApiClientError(`Failed to initiate authentication: ${response.statusText}`, response);
120+
throw await ApiClientError.fromResponse(response, `failed to initiate authentication`);
120121
}
121122

122123
return (await response.json()) as OauthDeviceCode;
@@ -147,14 +148,12 @@ export class ApiClient {
147148
try {
148149
const errorResponse = await response.json();
149150
if (errorResponse.errorCode === "DEVICE_AUTHORIZATION_PENDING") {
150-
throw new ApiClientError("Authentication pending. Try again later.", response);
151-
} else if (errorResponse.error === "expired_token") {
152-
throw new ApiClientError("Device code expired. Please restart the authentication process.", response);
151+
throw await ApiClientError.fromResponse(response, "Authentication pending. Try again later.");
153152
} else {
154-
throw new ApiClientError("Device code expired. Please restart the authentication process.", response);
153+
throw await ApiClientError.fromResponse(response, "Device code expired. Please restart the authentication process.");
155154
}
156155
} catch {
157-
throw new ApiClientError("Failed to retrieve token. Please check your device code.", response);
156+
throw await ApiClientError.fromResponse(response, "Failed to retrieve token. Please check your device code.");
158157
}
159158
}
160159

@@ -176,7 +175,7 @@ export class ApiClient {
176175
});
177176

178177
if (!response.ok) {
179-
throw new ApiClientError(`Failed to refresh token: ${response.statusText}`, response);
178+
throw await ApiClientError.fromResponse(response, "Failed to refresh token");
180179
}
181180
const data = await response.json();
182181

@@ -210,7 +209,7 @@ export class ApiClient {
210209
});
211210

212211
if (!response.ok) {
213-
throw new ApiClientError(`Failed to revoke token: ${response.statusText}`, response);
212+
throw await ApiClientError.fromResponse(response);
214213
}
215214

216215
if (!token && this.token) {

0 commit comments

Comments
 (0)