From 43a1d1016a33b9424f82fd19b4684b1af7135eed Mon Sep 17 00:00:00 2001 From: vben Date: Fri, 18 Oct 2024 21:49:18 +0800 Subject: [PATCH] perf: improved exception handling when request status code is 200 --- apps/web-antd/src/api/request.ts | 6 ++++-- apps/web-ele/src/api/request.ts | 5 +++-- apps/web-naive/src/api/request.ts | 5 +++-- docs/src/en/guide/essentials/server.md | 5 +++-- docs/src/guide/essentials/server.md | 5 +++-- playground/src/api/request.ts | 5 +++-- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/apps/web-antd/src/api/request.ts b/apps/web-antd/src/api/request.ts index e95f24662c0..83eef534f7e 100644 --- a/apps/web-antd/src/api/request.ts +++ b/apps/web-antd/src/api/request.ts @@ -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; }, }); diff --git a/apps/web-ele/src/api/request.ts b/apps/web-ele/src/api/request.ts index ea85965b50a..438b4bdd3ad 100644 --- a/apps/web-ele/src/api/request.ts +++ b/apps/web-ele/src/api/request.ts @@ -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; }, }); diff --git a/apps/web-naive/src/api/request.ts b/apps/web-naive/src/api/request.ts index 5f431ddee2c..6be11a11cd5 100644 --- a/apps/web-naive/src/api/request.ts +++ b/apps/web-naive/src/api/request.ts @@ -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; }, }); diff --git a/docs/src/en/guide/essentials/server.md b/docs/src/en/guide/essentials/server.md index d9b08011c82..2ef5a551fbf 100644 --- a/docs/src/en/guide/essentials/server.md +++ b/docs/src/en/guide/essentials/server.md @@ -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; }, }); diff --git a/docs/src/guide/essentials/server.md b/docs/src/guide/essentials/server.md index 29af40d1fdd..d01a1559dce 100644 --- a/docs/src/guide/essentials/server.md +++ b/docs/src/guide/essentials/server.md @@ -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; }, }); diff --git a/playground/src/api/request.ts b/playground/src/api/request.ts index 72617d1fc63..54227c60794 100644 --- a/playground/src/api/request.ts +++ b/playground/src/api/request.ts @@ -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; }, });