From 5d64f2f4f1162d0b8ec9371d1ba4a17586e32008 Mon Sep 17 00:00:00 2001 From: codev99 Date: Tue, 22 Oct 2024 17:38:37 +0200 Subject: [PATCH] feat: 124 api error status handler --- public/config/config.json | 8 ++++---- src/api/apiSlice.ts | 18 +++++++++--------- src/utils/useCatchError.tsx | 32 +++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/public/config/config.json b/public/config/config.json index 8bbdcad..96b8fcd 100644 --- a/public/config/config.json +++ b/public/config/config.json @@ -1,9 +1,9 @@ { "api": { - "AUTHN_API_BASE_URL": "http://135.236.145.79:8082", - "BFF_API_BASE_URL": "http://135.236.149.62:8081", - "EVENTS_API_BASE_URL": "http://48.209.179.60:8083", - "EVENTS_PUSH_API_BASE_URL": "http://48.209.179.60:8083", + "AUTHN_API_BASE_URL": "http://48.209.154.194:8082", + "BFF_API_BASE_URL": "http://48.209.154.250:8081", + "EVENTS_API_BASE_URL": "http://48.209.155.29:8083", + "EVENTS_PUSH_API_BASE_URL": "http://48.209.155.29:8083", "TERMINAL_SOCKET_URL": "http://localhost:8084" }, "params": { diff --git a/src/api/apiSlice.ts b/src/api/apiSlice.ts index 04ed96c..3a20424 100644 --- a/src/api/apiSlice.ts +++ b/src/api/apiSlice.ts @@ -4,16 +4,16 @@ const baseQuery = fetchBaseQuery({}); const customFetchBaseQuery: BaseQueryFn = async (args, api, extraOptions) => { const result = await baseQuery(args, api, extraOptions); - const realErrorCode = (result as unknown as {data: { code: number }})?.data?.code; - const realErrorMessage = (result as unknown as {data: { error: string }})?.data?.error; + // const realErrorCode = (result as unknown as {data: { code: number }})?.data?.code; + // const realErrorMessage = (result as unknown as {data: { error: string }})?.data?.error; - if (realErrorCode !== undefined && realErrorCode !== 200) { - const clientErrorRegex = /^4\d{2}$/; // Regex for 4xx client errors - if (clientErrorRegex.test(String(realErrorCode))) { - // use transformation to ovewrite the fetch status with right error code - throw new Error(JSON.stringify({data: {message: realErrorMessage, code: realErrorCode}})); - } - } + // if (realErrorCode !== undefined && realErrorCode !== 200) { + // const clientErrorRegex = /^4\d{2}$/; // Regex for 4xx client errors + // if (clientErrorRegex.test(String(realErrorCode))) { + // // use transformation to ovewrite the fetch status with right error code + // throw new Error(JSON.stringify({data: {message: realErrorMessage, code: realErrorCode}})); + // } + // } return result; } diff --git a/src/utils/useCatchError.tsx b/src/utils/useCatchError.tsx index ef1046f..dbee6ca 100644 --- a/src/utils/useCatchError.tsx +++ b/src/utils/useCatchError.tsx @@ -10,10 +10,11 @@ const useCatchError = () => { const catchError = (error?: SerializedError | FetchBaseQueryError | any, type: "result" | "notification" = "notification") => { let message: string = "Ops! Something didn't work"; let description: string = "Unable to complete the operation, please try later"; + + /* // Adjust to account for potentially nested error structure let actualErrorCode: number; let actualErrorMessage: string = ""; - const dispatch = useAppDispatch(); if (error?.message && JSON.parse(error.message).data?.code) { // error from API with status 200 @@ -46,6 +47,35 @@ const useCatchError = () => { // classic catch with error object message = actualErrorMessage; // override message only } + */ + + if (error?.status === 401) { + // logout + const dispatch = useAppDispatch(); + dispatch(logout()); + message = "Your session has expired"; + description = "Sign in again" + + } else if (error?.status === 500) { + // critical error + message = "Internal Server Error"; + description = "The server encountered an unexpected condition."; + + } else if ((/^4\d{2}$/).test(String(error?.status))) { + if (error?.error) { + // override error message + message = error.error; + } + if (error?.message) { + // override error message + message = error.message + } + description = "There was an error processing your request. Please check your input or permissions."; + + } else if (error?.message) { + // override error message + message = error.message + } switch (type) { case "result":