Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 124 api error status handler #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions public/config/config.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
18 changes: 9 additions & 9 deletions src/api/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const baseQuery = fetchBaseQuery({});

const customFetchBaseQuery: BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError> = 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;
}

Expand Down
32 changes: 31 additions & 1 deletion src/utils/useCatchError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down
Loading