Skip to content

Commit 7ea9854

Browse files
committed
fix: ApiClientError constructor
1 parent b85e884 commit 7ea9854

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
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 new ApiClientError("error calling Atlas API", response, error);
98+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
147+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
155+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
163+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
171+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
178+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
186+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
194+
throw ApiClientError.fromApiError(response, error);
195195
}
196196
return data;
197197
}
@@ -202,22 +202,22 @@ export class ApiClient {
202202
options
203203
);
204204
if (error) {
205-
throw new ApiClientError("error calling Atlas API", response, error);
205+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
212+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
220+
throw ApiClientError.fromApiError(response, error);
221221
}
222222
return data;
223223
}
@@ -228,7 +228,7 @@ export class ApiClient {
228228
options
229229
);
230230
if (error) {
231-
throw new ApiClientError("error calling Atlas API", response, error);
231+
throw ApiClientError.fromApiError(response, error);
232232
}
233233
}
234234

@@ -238,7 +238,7 @@ export class ApiClient {
238238
options
239239
);
240240
if (error) {
241-
throw new ApiClientError("error calling Atlas API", response, error);
241+
throw ApiClientError.fromApiError(response, error);
242242
}
243243
return data;
244244
}
@@ -249,7 +249,7 @@ export class ApiClient {
249249
options
250250
);
251251
if (error) {
252-
throw new ApiClientError("error calling Atlas API", response, error);
252+
throw ApiClientError.fromApiError(response, error);
253253
}
254254
return data;
255255
}
@@ -260,7 +260,7 @@ export class ApiClient {
260260
options
261261
);
262262
if (error) {
263-
throw new ApiClientError("error calling Atlas API", response, error);
263+
throw ApiClientError.fromApiError(response, error);
264264
}
265265
return data;
266266
}
@@ -271,22 +271,22 @@ export class ApiClient {
271271
options
272272
);
273273
if (error) {
274-
throw new ApiClientError("error calling Atlas API", response, error);
274+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
281+
throw ApiClientError.fromApiError(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 new ApiClientError("error calling Atlas API", response, error);
289+
throw ApiClientError.fromApiError(response, error);
290290
}
291291
return data;
292292
}

src/common/atlas/apiClientError.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { ApiError } from "./openapi.js";
22

33
export class ApiClientError extends Error {
4-
constructor(
4+
private constructor(
55
message: string,
6-
public readonly response?: Response,
7-
public readonly body?: ApiError
6+
public readonly apiError?: ApiError
87
) {
98
super(message);
109
this.name = "ApiClientError";
1110
}
1211

12+
static fromApiError(
13+
response: Response,
14+
apiError: ApiError,
15+
message: string = `error calling Atlas API`
16+
): ApiClientError {
17+
const errorMessage = this.buildErrorMessage(apiError);
18+
19+
return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, apiError);
20+
}
21+
1322
static async fromResponse(
1423
response: Response,
1524
message: string = `error calling Atlas API`
@@ -20,11 +29,7 @@ export class ApiClientError extends Error {
2029

2130
const body = err && typeof err === "object" ? err : undefined;
2231

23-
return new ApiClientError(
24-
`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`,
25-
response,
26-
body
27-
);
32+
return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, body);
2833
}
2934

3035
private static async extractError(response: Response): Promise<ApiError | string | undefined> {

0 commit comments

Comments
 (0)