Skip to content

Commit 5077fc0

Browse files
committed
fix: type errors
1 parent 03dea63 commit 5077fc0

File tree

3 files changed

+190
-61
lines changed

3 files changed

+190
-61
lines changed

src/common/atlas/apiClient.ts

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ export class ApiClient {
5555
},
5656
};
5757

58-
private readonly errorMiddleware: Middleware = {
59-
async onResponse({ response }) {
60-
if (!response.ok) {
61-
throw await ApiClientError.fromResponse(response);
62-
}
63-
},
64-
};
65-
6658
constructor(options: ApiClientOptions) {
6759
this.options = {
6860
...options,
@@ -91,7 +83,6 @@ export class ApiClient {
9183
});
9284
this.client.use(this.authMiddleware);
9385
}
94-
this.client.use(this.errorMiddleware);
9586
}
9687

9788
public hasCredentials(): boolean {
@@ -151,83 +142,152 @@ export class ApiClient {
151142

152143
// DO NOT EDIT. This is auto-generated code.
153144
async listClustersForAllProjects(options?: FetchOptions<operations["listClustersForAllProjects"]>) {
154-
const { data } = await this.client.GET("/api/atlas/v2/clusters", options);
145+
const { data, error, response } = await this.client.GET("/api/atlas/v2/clusters", options);
146+
if (error) {
147+
throw new ApiClientError("error calling Atlas API", response, error);
148+
}
155149
return data;
156150
}
157151

158152
async listProjects(options?: FetchOptions<operations["listProjects"]>) {
159-
const { data } = await this.client.GET("/api/atlas/v2/groups", options);
153+
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups", options);
154+
if (error) {
155+
throw new ApiClientError("error calling Atlas API", response, error);
156+
}
160157
return data;
161158
}
162159

163160
async createProject(options: FetchOptions<operations["createProject"]>) {
164-
const { data } = await this.client.POST("/api/atlas/v2/groups", options);
161+
const { data, error, response } = await this.client.POST("/api/atlas/v2/groups", options);
162+
if (error) {
163+
throw new ApiClientError("error calling Atlas API", response, error);
164+
}
165165
return data;
166166
}
167167

168168
async deleteProject(options: FetchOptions<operations["deleteProject"]>) {
169-
await this.client.DELETE("/api/atlas/v2/groups/{groupId}", options);
169+
const { error, response } = await this.client.DELETE("/api/atlas/v2/groups/{groupId}", options);
170+
if (error) {
171+
throw new ApiClientError("error calling Atlas API", response, error);
172+
}
170173
}
171174

172175
async getProject(options: FetchOptions<operations["getProject"]>) {
173-
const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}", options);
176+
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}", options);
177+
if (error) {
178+
throw new ApiClientError("error calling Atlas API", response, error);
179+
}
174180
return data;
175181
}
176182

177183
async listProjectIpAccessLists(options: FetchOptions<operations["listProjectIpAccessLists"]>) {
178-
const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/accessList", options);
184+
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}/accessList", options);
185+
if (error) {
186+
throw new ApiClientError("error calling Atlas API", response, error);
187+
}
179188
return data;
180189
}
181190

182191
async createProjectIpAccessList(options: FetchOptions<operations["createProjectIpAccessList"]>) {
183-
const { data } = await this.client.POST("/api/atlas/v2/groups/{groupId}/accessList", options);
192+
const { data, error, response } = await this.client.POST("/api/atlas/v2/groups/{groupId}/accessList", options);
193+
if (error) {
194+
throw new ApiClientError("error calling Atlas API", response, error);
195+
}
184196
return data;
185197
}
186198

187199
async deleteProjectIpAccessList(options: FetchOptions<operations["deleteProjectIpAccessList"]>) {
188-
await this.client.DELETE("/api/atlas/v2/groups/{groupId}/accessList/{entryValue}", options);
200+
const { error, response } = await this.client.DELETE(
201+
"/api/atlas/v2/groups/{groupId}/accessList/{entryValue}",
202+
options
203+
);
204+
if (error) {
205+
throw new ApiClientError("error calling Atlas API", response, error);
206+
}
189207
}
190208

191209
async listClusters(options: FetchOptions<operations["listClusters"]>) {
192-
const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/clusters", options);
210+
const { data, error, response } = await this.client.GET("/api/atlas/v2/groups/{groupId}/clusters", options);
211+
if (error) {
212+
throw new ApiClientError("error calling Atlas API", response, error);
213+
}
193214
return data;
194215
}
195216

196217
async createCluster(options: FetchOptions<operations["createCluster"]>) {
197-
const { data } = await this.client.POST("/api/atlas/v2/groups/{groupId}/clusters", options);
218+
const { data, error, response } = await this.client.POST("/api/atlas/v2/groups/{groupId}/clusters", options);
219+
if (error) {
220+
throw new ApiClientError("error calling Atlas API", response, error);
221+
}
198222
return data;
199223
}
200224

201225
async deleteCluster(options: FetchOptions<operations["deleteCluster"]>) {
202-
await this.client.DELETE("/api/atlas/v2/groups/{groupId}/clusters/{clusterName}", options);
226+
const { error, response } = await this.client.DELETE(
227+
"/api/atlas/v2/groups/{groupId}/clusters/{clusterName}",
228+
options
229+
);
230+
if (error) {
231+
throw new ApiClientError("error calling Atlas API", response, error);
232+
}
203233
}
204234

205235
async getCluster(options: FetchOptions<operations["getCluster"]>) {
206-
const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/clusters/{clusterName}", options);
236+
const { data, error, response } = await this.client.GET(
237+
"/api/atlas/v2/groups/{groupId}/clusters/{clusterName}",
238+
options
239+
);
240+
if (error) {
241+
throw new ApiClientError("error calling Atlas API", response, error);
242+
}
207243
return data;
208244
}
209245

210246
async listDatabaseUsers(options: FetchOptions<operations["listDatabaseUsers"]>) {
211-
const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/databaseUsers", options);
247+
const { data, error, response } = await this.client.GET(
248+
"/api/atlas/v2/groups/{groupId}/databaseUsers",
249+
options
250+
);
251+
if (error) {
252+
throw new ApiClientError("error calling Atlas API", response, error);
253+
}
212254
return data;
213255
}
214256

215257
async createDatabaseUser(options: FetchOptions<operations["createDatabaseUser"]>) {
216-
const { data } = await this.client.POST("/api/atlas/v2/groups/{groupId}/databaseUsers", options);
258+
const { data, error, response } = await this.client.POST(
259+
"/api/atlas/v2/groups/{groupId}/databaseUsers",
260+
options
261+
);
262+
if (error) {
263+
throw new ApiClientError("error calling Atlas API", response, error);
264+
}
217265
return data;
218266
}
219267

220268
async deleteDatabaseUser(options: FetchOptions<operations["deleteDatabaseUser"]>) {
221-
await this.client.DELETE("/api/atlas/v2/groups/{groupId}/databaseUsers/{databaseName}/{username}", options);
269+
const { error, response } = await this.client.DELETE(
270+
"/api/atlas/v2/groups/{groupId}/databaseUsers/{databaseName}/{username}",
271+
options
272+
);
273+
if (error) {
274+
throw new ApiClientError("error calling Atlas API", response, error);
275+
}
222276
}
223277

224278
async listOrganizations(options?: FetchOptions<operations["listOrganizations"]>) {
225-
const { data } = await this.client.GET("/api/atlas/v2/orgs", options);
279+
const { data, error, response } = await this.client.GET("/api/atlas/v2/orgs", options);
280+
if (error) {
281+
throw new ApiClientError("error calling Atlas API", response, error);
282+
}
226283
return data;
227284
}
228285

229286
async listOrganizationProjects(options: FetchOptions<operations["listOrganizationProjects"]>) {
230-
const { data } = await this.client.GET("/api/atlas/v2/orgs/{orgId}/groups", options);
287+
const { data, error, response } = await this.client.GET("/api/atlas/v2/orgs/{orgId}/groups", options);
288+
if (error) {
289+
throw new ApiClientError("error calling Atlas API", response, error);
290+
}
231291
return data;
232292
}
233293

src/common/atlas/apiClientError.ts

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

33
export class ApiClientError extends Error {
4-
private constructor(
4+
constructor(
55
message: string,
66
public readonly response?: Response,
77
public readonly body?: ApiError
@@ -14,35 +14,47 @@ export class ApiClientError extends Error {
1414
response: Response,
1515
message: string = `error calling Atlas API`
1616
): Promise<ApiClientError> {
17-
const { errorMessage, body } = await this.extractErrorMessage(response);
17+
const err = await this.extractError(response);
1818

19-
return new ApiClientError(`${message}: ${errorMessage}`, response, body);
19+
const errorMessage = this.buildErrorMessage(err);
20+
21+
const body = err && typeof err === "object" ? err : undefined;
22+
23+
return new ApiClientError(
24+
`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`,
25+
response,
26+
body
27+
);
2028
}
2129

22-
private static async extractErrorMessage(
23-
response: Response
24-
): Promise<{ errorMessage: string; body: ApiError | undefined }> {
25-
let errorMessage: string = "";
26-
let body: ApiError | undefined = undefined;
30+
private static async extractError(response: Response): Promise<ApiError | string | undefined> {
2731
try {
28-
body = (await response.json()) as ApiError;
29-
errorMessage = body.reason || "unknown error";
30-
if (body.detail && body.detail.length > 0) {
31-
errorMessage = `${errorMessage}; ${body.detail}`;
32-
}
32+
return (await response.json()) as ApiError;
3333
} catch {
3434
try {
35-
errorMessage = await response.text();
35+
return await response.text();
3636
} catch {
37-
errorMessage = "unknown error";
37+
return undefined;
3838
}
3939
}
40+
}
41+
42+
private static buildErrorMessage(error?: string | ApiError): string {
43+
let errorMessage: string = "unknown error";
4044

41-
errorMessage = `[${response.status} ${response.statusText}] ${errorMessage.trim()}`;
45+
//eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
46+
switch (typeof error) {
47+
case "object":
48+
errorMessage = error.reason || "unknown error";
49+
if (error.detail && error.detail.length > 0) {
50+
errorMessage = `${errorMessage}; ${error.detail}`;
51+
}
52+
break;
53+
case "string":
54+
errorMessage = error;
55+
break;
56+
}
4257

43-
return {
44-
errorMessage,
45-
body,
46-
};
58+
return errorMessage.trim();
4759
}
4860
}

0 commit comments

Comments
 (0)