Skip to content

Commit 0d1f745

Browse files
Remove try catch wrapper
1 parent b19c478 commit 0d1f745

File tree

1 file changed

+43
-48
lines changed

1 file changed

+43
-48
lines changed

src/index.ts

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,60 +33,55 @@ function getCorrectUrl(baseURL: string | undefined, url: string | undefined, par
3333

3434
export default function axiosAdapter<T>() {
3535
return async (config: AxiosRequestConfig): Promise<AxiosResponse<T>> => {
36-
try {
37-
38-
// Use filter to help remove undefined headers
39-
const headers = Object.entries(config.headers || {})
40-
.filter(([, value]) => value !== undefined);
41-
42-
const requestData = {
43-
method: config.method?.toUpperCase(),
44-
url: getCorrectUrl(config.baseURL, config.url, config.params),
45-
headers,
46-
data: getCorrectBodyType(config.data),
47-
};
48-
49-
const rid = await invoke<number>("plugin:http|fetch", requestData);
50-
51-
interface FetchSendResponse {
52-
status: number;
53-
statusText: string;
54-
headers: [[string, string]];
55-
url: string;
56-
}
57-
58-
const response = await invoke<FetchSendResponse>("plugin:http|fetch_send", {rid});
36+
// Use filter to help remove undefined headers
37+
const headers = Object.entries(config.headers || {})
38+
.filter(([, value]) => value !== undefined);
39+
40+
const requestData = {
41+
method: config.method?.toUpperCase(),
42+
url: getCorrectUrl(config.baseURL, config.url, config.params),
43+
headers,
44+
data: getCorrectBodyType(config.data),
45+
};
46+
47+
const rid = await invoke<number>("plugin:http|fetch", requestData);
48+
49+
interface FetchSendResponse {
50+
status: number;
51+
statusText: string;
52+
headers: [[string, string]];
53+
url: string;
54+
}
5955

60-
const body = await invoke<number[]>("plugin:http|fetch_read_body", {rid});
56+
const response = await invoke<FetchSendResponse>("plugin:http|fetch_send", {rid});
6157

62-
const isOk = response.status >= 200 && response.status < 300;
58+
const body = await invoke<number[]>("plugin:http|fetch_read_body", {rid});
6359

64-
let stringData = new TextDecoder().decode(new Uint8Array(body));
60+
const isOk = response.status >= 200 && response.status < 300;
6561

66-
let data;
67-
try {
68-
data = (config.responseType === 'json' || config.responseType === undefined) ? JSON.parse(stringData) : stringData;
69-
} catch (e) {
70-
data = new TextDecoder().decode(new Uint8Array(body));
71-
}
62+
let stringData = new TextDecoder().decode(new Uint8Array(body));
7263

73-
const axiosResponse: AxiosResponse<T> = {
74-
data,
75-
status: response.status,
76-
statusText: getReasonPhrase(response.status),
77-
headers: Object.fromEntries(response.headers),
78-
config: config as InternalAxiosRequestConfig
79-
};
64+
let data;
65+
try {
66+
data = (config.responseType === 'json' || config.responseType === undefined) ? JSON.parse(stringData) : stringData;
67+
} catch (e) {
68+
data = new TextDecoder().decode(new Uint8Array(body));
69+
}
8070

81-
if (isOk) {
82-
return axiosResponse;
83-
} else {
84-
const code = [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4];
85-
const message = 'Request failed with status code ' + axiosResponse.status;
86-
throw new AxiosError(message, code, config as InternalAxiosRequestConfig, requestData, axiosResponse);
87-
}
88-
} catch (error) {
89-
throw error;
71+
const axiosResponse: AxiosResponse<T> = {
72+
data,
73+
status: response.status,
74+
statusText: getReasonPhrase(response.status),
75+
headers: Object.fromEntries(response.headers),
76+
config: config as InternalAxiosRequestConfig
77+
};
78+
79+
if (isOk) {
80+
return axiosResponse;
81+
} else {
82+
const code = [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4];
83+
const message = 'Request failed with status code ' + axiosResponse.status;
84+
throw new AxiosError(message, code, config as InternalAxiosRequestConfig, requestData, axiosResponse);
9085
}
9186
}
9287
}

0 commit comments

Comments
 (0)