@@ -32,15 +32,16 @@ export class ApiClientError extends Error {
32
32
this . response = response ;
33
33
}
34
34
35
- static async fromResponse ( response : Response ) : Promise < ApiClientError > {
35
+ static async fromResponse ( response : Response , message ?: string ) : Promise < ApiClientError > {
36
+ message ||= `error calling Atlas API` ;
36
37
try {
37
38
const text = await response . text ( ) ;
38
39
return new ApiClientError (
39
- `Error calling Atlas API : [${ response . status } ${ response . statusText } ] ${ text } ` ,
40
+ `${ message } : [${ response . status } ${ response . statusText } ] ${ text } ` ,
40
41
response
41
42
) ;
42
43
} catch {
43
- return new ApiClientError ( `Error calling Atlas API : ${ response . status } ${ response . statusText } ` , response ) ;
44
+ return new ApiClientError ( `${ message } : ${ response . status } ${ response . statusText } ` , response ) ;
44
45
}
45
46
}
46
47
}
@@ -116,7 +117,7 @@ export class ApiClient {
116
117
} ) ;
117
118
118
119
if ( ! response . ok ) {
119
- throw new ApiClientError ( `Failed to initiate authentication: ${ response . statusText } ` , response ) ;
120
+ throw await ApiClientError . fromResponse ( response , `failed to initiate authentication` ) ;
120
121
}
121
122
122
123
return ( await response . json ( ) ) as OauthDeviceCode ;
@@ -147,14 +148,12 @@ export class ApiClient {
147
148
try {
148
149
const errorResponse = await response . json ( ) ;
149
150
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." ) ;
153
152
} 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." ) ;
155
154
}
156
155
} 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." ) ;
158
157
}
159
158
}
160
159
@@ -176,7 +175,7 @@ export class ApiClient {
176
175
} ) ;
177
176
178
177
if ( ! response . ok ) {
179
- throw new ApiClientError ( ` Failed to refresh token: ${ response . statusText } ` , response ) ;
178
+ throw await ApiClientError . fromResponse ( response , " Failed to refresh token" ) ;
180
179
}
181
180
const data = await response . json ( ) ;
182
181
@@ -210,7 +209,7 @@ export class ApiClient {
210
209
} ) ;
211
210
212
211
if ( ! response . ok ) {
213
- throw new ApiClientError ( `Failed to revoke token: ${ response . statusText } ` , response ) ;
212
+ throw await ApiClientError . fromResponse ( response ) ;
214
213
}
215
214
216
215
if ( ! token && this . token ) {
0 commit comments