π OpenHands: AI-Driven Development
chore(frontend): Better error toast handling mechanism (#6561)
sp.wack575f4fd347e6
diff --git a/frontend/src/hooks/query/use-active-host.ts b/frontend/src/hooks/query/use-active-host.tsindex 6a5f8ec017..6a2d2f7d88 100644--- a/frontend/src/hooks/query/use-active-host.ts+++ b/frontend/src/hooks/query/use-active-host.ts@@ -23,6 +23,9 @@ export const useActiveHost = () => {},enabled: !RUNTIME_INACTIVE_STATES.includes(curAgentState),initialData: { hosts: [] },+ meta: {+ disableToast: true,+ },});const apps = useQueries({@@ -37,6 +40,9 @@ export const useActiveHost = () => {}},refetchInterval: 3000,+ meta: {+ disableToast: true,+ },})),});diff --git a/frontend/src/hooks/query/use-is-authed.ts b/frontend/src/hooks/query/use-is-authed.tsindex 35987f7d9b..bb58993ce4 100644--- a/frontend/src/hooks/query/use-is-authed.ts+++ b/frontend/src/hooks/query/use-is-authed.ts@@ -16,5 +16,8 @@ export const useIsAuthed = () => {enabled: !!appMode,staleTime: 1000 * 60 * 5, // 5 minutesretry: false,+ meta: {+ disableToast: true,+ },});};diff --git a/frontend/src/hooks/query/use-settings.ts b/frontend/src/hooks/query/use-settings.tsindex 9d2226a64a..e9fca837f0 100644--- a/frontend/src/hooks/query/use-settings.ts+++ b/frontend/src/hooks/query/use-settings.ts@@ -32,6 +32,9 @@ export const useSettings = () => {initialData: DEFAULT_SETTINGS,staleTime: 0,retry: false,+ meta: {+ disableToast: true,+ },});React.useEffect(() => {diff --git a/frontend/src/query-client-config.ts b/frontend/src/query-client-config.tsindex afafcc62b1..fa9342ce45 100644--- a/frontend/src/query-client-config.ts+++ b/frontend/src/query-client-config.ts@@ -1,13 +1,22 @@import { QueryClientConfig, QueryCache } from "@tanstack/react-query";-import { renderToastIfError } from "./utils/render-toast-if-error";--const QUERY_KEYS_TO_IGNORE = ["authenticated", "hosts", "settings"];+import toast from "react-hot-toast";+import { retrieveAxiosErrorMessage } from "./utils/retrieve-axios-error-message";+const shownErrors = new Set<string>();export const queryClientConfig: QueryClientConfig = {queryCache: new QueryCache({onError: (error, query) => {- if (!QUERY_KEYS_TO_IGNORE.some((key) => query.queryKey.includes(key))) {- renderToastIfError(error);+ if (!query.meta?.disableToast) {+ const errorMessage = retrieveAxiosErrorMessage(error);++ if (!shownErrors.has(errorMessage)) {+ toast.error(errorMessage || "An error occurred");+ shownErrors.add(errorMessage);++ setTimeout(() => {+ shownErrors.delete(errorMessage);+ }, 3000);+ }}},}),@@ -18,7 +27,8 @@ export const queryClientConfig: QueryClientConfig = {},mutations: {onError: (error) => {- renderToastIfError(error);+ const message = retrieveAxiosErrorMessage(error);+ toast.error(message);},},},diff --git a/frontend/src/utils/render-toast-if-error.ts b/frontend/src/utils/retrieve-axios-error-message.tssimilarity index 63%rename from frontend/src/utils/render-toast-if-error.tsrename to frontend/src/utils/retrieve-axios-error-message.tsindex 08a4d74445..f24af0ef60 100644--- a/frontend/src/utils/render-toast-if-error.ts+++ b/frontend/src/utils/retrieve-axios-error-message.ts@@ -1,12 +1,11 @@import { AxiosError } from "axios";-import toast from "react-hot-toast";import { isAxiosErrorWithResponse } from "./type-guards";/**- * Renders a toast with the error message from an Axios error+ * Retrieve the error message from an Axios error* @param error The error to render a toast for*/-export const renderToastIfError = (error: AxiosError) => {+export const retrieveAxiosErrorMessage = (error: AxiosError) => {let errorMessage: string | null = null;if (isAxiosErrorWithResponse(error) && error.response?.data.error) {@@ -15,5 +14,5 @@ export const renderToastIfError = (error: AxiosError) => {errorMessage = error.message;}- toast.error(errorMessage || "An error occurred");+ return errorMessage || "An error occurred";};