Skip to content

Commit

Permalink
perf: improved exception handling when request status code is 200
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Oct 18, 2024
1 parent 6f3d509 commit 43a1d10
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
6 changes: 4 additions & 2 deletions apps/web-antd/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ function createRequestClient(baseURL: string) {
fulfilled: (response) => {
const { data: responseData, status } = response;

const { code, data, message: msg } = responseData;
const { code, data } = responseData;
if (status >= 200 && status < 400 && code === 0) {
return data;
}
throw new Error(`Error ${status}: ${msg}`);

const error = { response };
throw error;
},
});

Expand Down
5 changes: 3 additions & 2 deletions apps/web-ele/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ function createRequestClient(baseURL: string) {
fulfilled: (response) => {
const { data: responseData, status } = response;

const { code, data, message: msg } = responseData;
const { code, data } = responseData;
if (status >= 200 && status < 400 && code === 0) {
return data;
}
throw new Error(`Error ${status}: ${msg}`);
const error = { response };
throw error;
},
});

Expand Down
5 changes: 3 additions & 2 deletions apps/web-naive/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ function createRequestClient(baseURL: string) {
fulfilled: (response) => {
const { data: responseData, status } = response;

const { code, data, message: msg } = responseData;
const { code, data } = responseData;
if (status >= 200 && status < 400 && code === 0) {
return data;
}
throw new Error(`Error ${status}: ${msg}`);
const error = { response };
throw error;
},
});

Expand Down
5 changes: 3 additions & 2 deletions docs/src/en/guide/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,13 @@ function createRequestClient(baseURL: string) {
fulfilled: (response) => {
const { data: responseData, status } = response;

const { code, data, message: msg } = responseData;
const { code, data } = responseData;

if (status >= 200 && status < 400 && code === 0) {
return data;
}
throw new Error(`Error ${status}: ${msg}`);
const error = { response };
throw error;
},
});

Expand Down
5 changes: 3 additions & 2 deletions docs/src/guide/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,13 @@ function createRequestClient(baseURL: string) {
fulfilled: (response) => {
const { data: responseData, status } = response;

const { code, data, message: msg } = responseData;
const { code, data } = responseData;

if (status >= 200 && status < 400 && code === 0) {
return data;
}
throw new Error(`Error ${status}: ${msg}`);
const error = { response };
throw error;
},
});

Expand Down
5 changes: 3 additions & 2 deletions playground/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ function createRequestClient(baseURL: string) {
fulfilled: (response) => {
const { data: responseData, status } = response;

const { code, data, message: msg } = responseData;
const { code, data } = responseData;

if (status >= 200 && status < 400 && code === 0) {
return data;
}
throw new Error(`Error ${status}: ${msg}`);
const error = { response };
throw error;
},
});

Expand Down

0 comments on commit 43a1d10

Please sign in to comment.