Skip to content

Commit

Permalink
Merge pull request #1524 from jasneetsingh6114/feat/issue-1482
Browse files Browse the repository at this point in the history
Inform Admin incase of server issue.
  • Loading branch information
ajhollid authored Jan 10, 2025
2 parents 2dfe5de + 74f23c1 commit a2102ca
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions Client/src/Utils/NetworkService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
const FALLBACK_BASE_URL = "http://localhost:5000/api/v1";
import { clearAuthState } from "../Features/Auth/authSlice";
import { clearUptimeMonitorState } from "../Features/UptimeMonitors/uptimeMonitorsSlice";
import { createToast } from "./toastUtils";

class NetworkService {
constructor(store, dispatch, navigate) {
this.store = store;
Expand All @@ -25,11 +27,7 @@ class NetworkService {
this.axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && error.response.status === 401) {
dispatch(clearAuthState());
dispatch(clearUptimeMonitorState());
navigate("/login");
}
this.handleError(error);
return Promise.reject(error);
}
);
Expand All @@ -39,6 +37,30 @@ class NetworkService {
this.axiosInstance.defaults.baseURL = url;
};

handleError(error) {
if (error.response) {
const status = error.response.status;
if (status === 401) {
this.dispatch(clearAuthState());
this.dispatch(clearUptimeMonitorState());
this.navigate("/login");
} else if (status >= 500) {
// Display toast for server errors to all users
createToast({
variant: "error",
body: "Checkmate server is not running or has issues. Please check.",
});
}
} else if (error.request) {
// Show a toast informing the user the server didn't respond
createToast({
variant: "error",
body: "The server did not respond. Please check your network or try again later.",
});
}
}


cleanup() {
if (this.unsubscribe) {
this.unsubscribe();
Expand Down

0 comments on commit a2102ca

Please sign in to comment.