Skip to content

Commit fc79dd4

Browse files
feat: add raise_error option to API requests for better error handling
1 parent 94359e9 commit fc79dd4

File tree

7 files changed

+52
-24
lines changed

7 files changed

+52
-24
lines changed

src/lib/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export async function getLatestO11YBuildInfo(
1919
headers: {
2020
Authorization: `Basic ${auth}`,
2121
},
22+
raise_error: false,
2223
});
2324

2425
if (!buildsResponse.ok) {

src/lib/apiClient.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type RequestOptions = {
55
headers?: Record<string, string>;
66
params?: Record<string, string | number>;
77
body?: any;
8+
raise_error?: boolean; // default: true
89
};
910

1011
class ApiResponse<T = any> {
@@ -56,61 +57,79 @@ class ApiResponse<T = any> {
5657
class ApiClient {
5758
private instance = axios.create();
5859

60+
private async requestWrapper<T>(
61+
fn: () => Promise<AxiosResponse<T>>,
62+
raise_error: boolean = true,
63+
): Promise<ApiResponse<T>> {
64+
try {
65+
const res = await fn();
66+
return new ApiResponse<T>(res);
67+
} catch (error: any) {
68+
if (error.response && !raise_error) {
69+
return new ApiResponse<T>(error.response);
70+
}
71+
throw error;
72+
}
73+
}
74+
5975
async get<T = any>({
6076
url,
6177
headers,
6278
params,
79+
raise_error = true,
6380
}: RequestOptions): Promise<ApiResponse<T>> {
64-
const res = await this.instance.get<T>(url, {
65-
headers,
66-
params,
67-
});
68-
return new ApiResponse<T>(res);
81+
return this.requestWrapper<T>(
82+
() => this.instance.get<T>(url, { headers, params }),
83+
raise_error,
84+
);
6985
}
7086

7187
async post<T = any>({
7288
url,
7389
headers,
7490
body,
91+
raise_error = true,
7592
}: RequestOptions): Promise<ApiResponse<T>> {
76-
const res = await this.instance.post<T>(url, body, {
77-
headers,
78-
});
79-
return new ApiResponse<T>(res);
93+
return this.requestWrapper<T>(
94+
() => this.instance.post<T>(url, body, { headers }),
95+
raise_error,
96+
);
8097
}
8198

8299
async put<T = any>({
83100
url,
84101
headers,
85102
body,
103+
raise_error = true,
86104
}: RequestOptions): Promise<ApiResponse<T>> {
87-
const res = await this.instance.put<T>(url, body, {
88-
headers,
89-
});
90-
return new ApiResponse<T>(res);
105+
return this.requestWrapper<T>(
106+
() => this.instance.put<T>(url, body, { headers }),
107+
raise_error,
108+
);
91109
}
92110

93111
async patch<T = any>({
94112
url,
95113
headers,
96114
body,
115+
raise_error = true,
97116
}: RequestOptions): Promise<ApiResponse<T>> {
98-
const res = await this.instance.patch<T>(url, body, {
99-
headers,
100-
});
101-
return new ApiResponse<T>(res);
117+
return this.requestWrapper<T>(
118+
() => this.instance.patch<T>(url, body, { headers }),
119+
raise_error,
120+
);
102121
}
103122

104123
async delete<T = any>({
105124
url,
106125
headers,
107126
params,
127+
raise_error = true,
108128
}: RequestOptions): Promise<ApiResponse<T>> {
109-
const res = await this.instance.delete<T>(url, {
110-
headers,
111-
params,
112-
});
113-
return new ApiResponse<T>(res);
129+
return this.requestWrapper<T>(
130+
() => this.instance.delete<T>(url, { headers, params }),
131+
raise_error,
132+
);
114133
}
115134
}
116135

src/lib/device-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function getDevicesAndBrowsers(
4949
}
5050
}
5151

52-
const liveRes = await apiClient.get({ url: URLS[type] });
52+
const liveRes = await apiClient.get({ url: URLS[type], raise_error: false });
5353

5454
if (!liveRes.ok) {
5555
throw new Error(

src/tools/accessiblity-utils/accessibility-rag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export async function queryAccessibilityRAG(
3333
body: {
3434
query: userQuery,
3535
},
36+
raise_error: false,
3637
});
3738

3839
if (!response.ok) {

src/tools/failurelogs-utils/app-automate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export async function retrieveDeviceLogs(
1919
"Content-Type": "application/json",
2020
Authorization: `Basic ${auth}`,
2121
},
22+
raise_error: false,
2223
});
2324

2425
const validationError = validateLogResponse(response, "device logs");
@@ -50,6 +51,7 @@ export async function retrieveAppiumLogs(
5051
"Content-Type": "application/json",
5152
Authorization: `Basic ${auth}`,
5253
},
54+
raise_error: false,
5355
});
5456

5557
const validationError = validateLogResponse(response, "Appium logs");
@@ -81,6 +83,7 @@ export async function retrieveCrashLogs(
8183
"Content-Type": "application/json",
8284
Authorization: `Basic ${auth}`,
8385
},
86+
raise_error: false,
8487
});
8588

8689
const validationError = validateLogResponse(response, "crash logs");

src/tools/failurelogs-utils/automate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export async function retrieveNetworkFailures(
2323
"Content-Type": "application/json",
2424
Authorization: `Basic ${auth}`,
2525
},
26+
raise_error: false,
2627
});
2728

2829
const validationError = validateLogResponse(response, "network logs");
@@ -74,6 +75,7 @@ export async function retrieveSessionFailures(
7475
"Content-Type": "application/json",
7576
Authorization: `Basic ${auth}`,
7677
},
78+
raise_error: false,
7779
});
7880

7981
const validationError = validateLogResponse(response, "session logs");
@@ -104,6 +106,7 @@ export async function retrieveConsoleFailures(
104106
"Content-Type": "application/json",
105107
Authorization: `Basic ${auth}`,
106108
},
109+
raise_error: false,
107110
});
108111

109112
const validationError = validateLogResponse(response, "console logs");

src/tools/failurelogs-utils/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ApiResponse } from "../../lib/apiClient.js";
2+
13
export interface LogResponse {
24
logs?: any[];
35
message?: string;
@@ -25,7 +27,6 @@ export interface HarEntry {
2527
time?: number;
2628
}
2729

28-
import type { ApiResponse } from "../../lib/apiClient.js";
2930
export function validateLogResponse(
3031
response: Response | ApiResponse,
3132
logType: string,

0 commit comments

Comments
 (0)