Skip to content

Commit 84d70ef

Browse files
committed
fix: add Error type
1 parent 7ea9854 commit 84d70ef

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

scripts/apply.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async function main() {
9595
return `async ${operationId}(options${requiredParams ? "" : "?"}: FetchOptions<operations["${operationId}"]>) {
9696
const { ${hasResponseBody ? `data, ` : ``}error, response } = await this.client.${method}("${path}", options);
9797
if (error) {
98-
throw ApiClientError.fromApiError(response, error);
98+
throw ApiClientError.fromError(response, error);
9999
}
100100
${
101101
hasResponseBody

src/common/atlas/apiClient.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,54 +144,54 @@ export class ApiClient {
144144
async listClustersForAllProjects(options?: FetchOptions<operations["listClustersForAllProjects"]>) {
145145
const { data, error, response } = await this.client.GET("/api/atlas/v2/clusters", options);
146146
if (error) {
147-
throw ApiClientError.fromApiError(response, error);
147+
throw ApiClientError.fromError(response, error);
148148
}
149149
return data;
150150
}
151151

152152
async listProjects(options?: FetchOptions<operations["listProjects"]>) {
153153
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups", options);
154154
if (error) {
155-
throw ApiClientError.fromApiError(response, error);
155+
throw ApiClientError.fromError(response, error);
156156
}
157157
return data;
158158
}
159159

160160
async createProject(options: FetchOptions<operations["createProject"]>) {
161161
const { data, error, response } = await this.client.POST("/api/atlas/v2/groups", options);
162162
if (error) {
163-
throw ApiClientError.fromApiError(response, error);
163+
throw ApiClientError.fromError(response, error);
164164
}
165165
return data;
166166
}
167167

168168
async deleteProject(options: FetchOptions<operations["deleteProject"]>) {
169169
const { error, response } = await this.client.DELETE("/api/atlas/v2/groups/{groupId}", options);
170170
if (error) {
171-
throw ApiClientError.fromApiError(response, error);
171+
throw ApiClientError.fromError(response, error);
172172
}
173173
}
174174

175175
async getProject(options: FetchOptions<operations["getProject"]>) {
176176
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}", options);
177177
if (error) {
178-
throw ApiClientError.fromApiError(response, error);
178+
throw ApiClientError.fromError(response, error);
179179
}
180180
return data;
181181
}
182182

183183
async listProjectIpAccessLists(options: FetchOptions<operations["listProjectIpAccessLists"]>) {
184184
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}/accessList", options);
185185
if (error) {
186-
throw ApiClientError.fromApiError(response, error);
186+
throw ApiClientError.fromError(response, error);
187187
}
188188
return data;
189189
}
190190

191191
async createProjectIpAccessList(options: FetchOptions<operations["createProjectIpAccessList"]>) {
192192
const { data, error, response } = await this.client.POST("/api/atlas/v2/groups/{groupId}/accessList", options);
193193
if (error) {
194-
throw ApiClientError.fromApiError(response, error);
194+
throw ApiClientError.fromError(response, error);
195195
}
196196
return data;
197197
}
@@ -202,22 +202,22 @@ export class ApiClient {
202202
options
203203
);
204204
if (error) {
205-
throw ApiClientError.fromApiError(response, error);
205+
throw ApiClientError.fromError(response, error);
206206
}
207207
}
208208

209209
async listClusters(options: FetchOptions<operations["listClusters"]>) {
210210
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}/clusters", options);
211211
if (error) {
212-
throw ApiClientError.fromApiError(response, error);
212+
throw ApiClientError.fromError(response, error);
213213
}
214214
return data;
215215
}
216216

217217
async createCluster(options: FetchOptions<operations["createCluster"]>) {
218218
const { data, error, response } = await this.client.POST("/api/atlas/v2/groups/{groupId}/clusters", options);
219219
if (error) {
220-
throw ApiClientError.fromApiError(response, error);
220+
throw ApiClientError.fromError(response, error);
221221
}
222222
return data;
223223
}
@@ -228,7 +228,7 @@ export class ApiClient {
228228
options
229229
);
230230
if (error) {
231-
throw ApiClientError.fromApiError(response, error);
231+
throw ApiClientError.fromError(response, error);
232232
}
233233
}
234234

@@ -238,7 +238,7 @@ export class ApiClient {
238238
options
239239
);
240240
if (error) {
241-
throw ApiClientError.fromApiError(response, error);
241+
throw ApiClientError.fromError(response, error);
242242
}
243243
return data;
244244
}
@@ -249,7 +249,7 @@ export class ApiClient {
249249
options
250250
);
251251
if (error) {
252-
throw ApiClientError.fromApiError(response, error);
252+
throw ApiClientError.fromError(response, error);
253253
}
254254
return data;
255255
}
@@ -260,7 +260,7 @@ export class ApiClient {
260260
options
261261
);
262262
if (error) {
263-
throw ApiClientError.fromApiError(response, error);
263+
throw ApiClientError.fromError(response, error);
264264
}
265265
return data;
266266
}
@@ -271,22 +271,22 @@ export class ApiClient {
271271
options
272272
);
273273
if (error) {
274-
throw ApiClientError.fromApiError(response, error);
274+
throw ApiClientError.fromError(response, error);
275275
}
276276
}
277277

278278
async listOrganizations(options?: FetchOptions<operations["listOrganizations"]>) {
279279
const { data, error, response } = await this.client.GET("/api/atlas/v2/orgs", options);
280280
if (error) {
281-
throw ApiClientError.fromApiError(response, error);
281+
throw ApiClientError.fromError(response, error);
282282
}
283283
return data;
284284
}
285285

286286
async listOrganizationProjects(options: FetchOptions<operations["listOrganizationProjects"]>) {
287287
const { data, error, response } = await this.client.GET("/api/atlas/v2/orgs/{orgId}/groups", options);
288288
if (error) {
289-
throw ApiClientError.fromApiError(response, error);
289+
throw ApiClientError.fromError(response, error);
290290
}
291291
return data;
292292
}

src/common/atlas/apiClientError.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,25 @@ export class ApiClientError extends Error {
99
this.name = "ApiClientError";
1010
}
1111

12-
static fromApiError(
12+
static async fromResponse(
1313
response: Response,
14-
apiError: ApiError,
1514
message: string = `error calling Atlas API`
16-
): ApiClientError {
17-
const errorMessage = this.buildErrorMessage(apiError);
15+
): Promise<ApiClientError> {
16+
const err = await this.extractError(response);
1817

19-
return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, apiError);
18+
return this.fromError(response, err, message);
2019
}
2120

22-
static async fromResponse(
21+
private static fromError(
2322
response: Response,
23+
error?: ApiError | string | Error,
2424
message: string = `error calling Atlas API`
25-
): Promise<ApiClientError> {
26-
const err = await this.extractError(response);
27-
28-
const errorMessage = this.buildErrorMessage(err);
25+
): ApiClientError {
26+
const errorMessage = this.buildErrorMessage(error);
2927

30-
const body = err && typeof err === "object" ? err : undefined;
28+
const apiError = typeof error === "object" ? error : undefined;
3129

32-
return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, body);
30+
return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, apiError);
3331
}
3432

3533
private static async extractError(response: Response): Promise<ApiError | string | undefined> {
@@ -44,9 +42,13 @@ export class ApiClientError extends Error {
4442
}
4543
}
4644

47-
private static buildErrorMessage(error?: string | ApiError): string {
45+
private static buildErrorMessage(error?: string | ApiError | Error): string {
4846
let errorMessage: string = "unknown error";
4947

48+
if (error instanceof Error) {
49+
return error.message;
50+
}
51+
5052
//eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
5153
switch (typeof error) {
5254
case "object":

0 commit comments

Comments
 (0)