Skip to content

Commit

Permalink
fix: improve error handling around axios errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe authored and moshloop committed Sep 3, 2024
1 parent ba21f91 commit 0cb1068
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
22 changes: 19 additions & 3 deletions src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toastError } from "@flanksource-ui/components/Toast/toast";
import axios, { AxiosError } from "axios";
import { toastError } from "../components/Toast/toast";

const isClerkAuthSystem = !!process.env.NEXT_PUBLIC_AUTH_IS_CLERK === true;

Expand Down Expand Up @@ -146,10 +146,26 @@ for (const client of [
Snapshot
]) {
client.interceptors.response.use(
(response) => response,
(response) => {
return response;
},
(error) => {
redirectToLoginPageOnSessionExpiry(error);
toastError(error.response.data.message);
if ("code" in (error as any)) {
// Show a toast message for timeout and network errors
if (error.code === "ECONNABORTED") {
toastError("Request timed out. Please try again.");
return;
}
if (error.code === "ERR_NETWORK") {
toastError("Network Error. Please try again.");
return;
}
if (error.code === "ERR_INTERNET_DISCONNECTED") {
toastError("No internet connection. Please try again.");
return;
}
}
return Promise.reject(error);
}
);
Expand Down
23 changes: 6 additions & 17 deletions src/api/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,10 @@ export type ApiResp<T = any> = Promise<
export const resolvePostGrestRequestWithPagination: <T>(
promise: AxiosPromise<T>
) => ApiResp<T> = async (promise) => {
try {
const { data, headers } = await promise;
const hasContentRangeHeader = !!headers["content-range"]?.trim();
const totalEntries = hasContentRangeHeader
? +headers["content-range"].split("/")[1]
: undefined;
return { data, error: null, totalEntries };
} catch (error: any) {
if (error instanceof Error) {
return { error, data: null };
} else {
return {
error: Error("Unknown error happened while fetching."),
data: null
};
}
}
const { data, headers } = await promise;
const hasContentRangeHeader = !!headers["content-range"]?.trim();
const totalEntries = hasContentRangeHeader
? +headers["content-range"].split("/")[1]
: undefined;
return { data, error: null, totalEntries };
};
8 changes: 1 addition & 7 deletions src/query-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { QueryClient } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { toastError } from "./components/Toast/toast";

const defaultStaleTime = 1000 * 60 * 5;

Expand All @@ -14,18 +13,13 @@ export const queryClient = new QueryClient({
retry: (failureCount, error) => {
if (error instanceof AxiosError) {
// Retry timeout errors and network errors with a delay for up to 3 times
if (error.code === "ECONNABORTED" || !error.response) {
if (error.code === "ECONNABORTED" || error.code === "ERR_NETWORK") {
if (failureCount < 3) {
return true;
}
}
}
return false;
},
onError: (error) => {
if (error instanceof Error) {
toastError(`Something went wrong: ${error.message}`);
}
}
}
}
Expand Down

0 comments on commit 0cb1068

Please sign in to comment.