From 9b3c1fb49bf9dae7f862b0ed5efbb65f75257af7 Mon Sep 17 00:00:00 2001 From: Moaz Dawalibi Date: Thu, 26 Sep 2024 10:47:22 +0300 Subject: [PATCH] fix react query bug --- src/api/helper/useAxios.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/api/helper/useAxios.ts b/src/api/helper/useAxios.ts index a269a92..e3b4e5d 100644 --- a/src/api/helper/useAxios.ts +++ b/src/api/helper/useAxios.ts @@ -34,10 +34,10 @@ function useAxios() { function (response: any) { const responseMsg = response?.data?.message; const method = response.config.method; - + const key = response.config.headers[HEADER_KEY]; const isToasted = response.config.headers["X-Custom-Message"]; - + const ResponseMessage = responseMsg || t("validation.the_possess_done_successful"); if (method !== AxiosQueryEnum?.GET) { @@ -50,13 +50,16 @@ function useAxios() { return response; }, function (error) { - console.log(error?.response); - - const status = error?.request?.status; + // Reject errors with non-2xx status codes + const status = error?.response?.status; + if (status >= 400) { + return Promise.reject(error); + } + const errorMsg = error?.response?.data?.error; const errorField = error?.response?.data; const method = error.config.method; - + if (status === AxiosStatusEnum.VALIDATION) { setValidation(errorMsg ?? errorField); const ErrorKey = Object?.keys(errorMsg)?.[0]; @@ -65,8 +68,6 @@ function useAxios() { ? errorMsg : errorMsg?.[ErrorKey]?.[0] ?? t("validation.some_thing_went_wrong"); - console.log(isString); - toast.error(t(`${isString}`)); return; } @@ -74,14 +75,17 @@ function useAxios() { logout(); navigate("/auth"); } - + if (method !== AxiosQueryEnum?.GET) { const errorMessage = errorMsg || t("validation.some_thing_went_wrong"); toast.error(errorMessage); return Promise.reject(error); } - }, + + return Promise.reject(error); // Important to reject the promise + } ); + return build_Axios; // return buildAxios.build();