Replies: 10 comments 8 replies
-
There's no way right now to try/catch fetch errors, I suppose Remix should do it and let you know if something failed with a value returned by useFetcher or useSubmit. Something you can do right now, is to prevent the submit if the user is not online, you can detect that with the navigator.onLine API. |
Beta Was this translation helpful? Give feedback.
-
My current workaround is to use the raw fetcher and handle aborting on unmount. I'm using the This implementation assumes you are just refetching from the page's loader, but you could pass in params just like the import { useLocation, useMatches } from "@remix-run/react";
import { useCallback, useEffect, useMemo, useState } from "react";
import useFetch, { CachePolicies } from "use-http";
export const useFetcherRaw = <T>() => {
const [data, setData] = useState<T>();
const [error, setError] = useState<Error>();
const fetcher = useFetch<T>({
cachePolicy: CachePolicies.NO_CACHE,
onNewData: useCallback((_, newData) => setData(newData), [setData]),
});
const location = useLocation();
const matches = useMatches();
const get = useCallback(() => {
const route = matches.find((match) => match.pathname === location.pathname);
if (route) {
fetcher.get(
`${location.pathname}?${new URLSearchParams({
_data: route.id,
method: "GET",
})}`
);
}
}, [fetcher, location, matches]);
// Ensure errors are not cleared until there is a succesful load
useEffect(() => {
if (fetcher.error) {
setError(fetcher.error);
}
if (!fetcher.error && !fetcher.loading) {
setError(fetcher.error);
}
}, [fetcher.error, fetcher.loading]);
return useMemo(
() => ({
get,
data,
loading: fetcher.loading,
error: error,
}),
[get, fetcher, data, error]
);
}; Are there any reasons not to do this? |
Beta Was this translation helpful? Give feedback.
-
Does this mean you cant catch errors in components that utilise useFetcher?? |
Beta Was this translation helpful? Give feedback.
-
Any news on this 🧵? |
Beta Was this translation helpful? Give feedback.
-
I'm now playing with the following workaround (or solution?): // ErrorBoundary equals to your main component
export let ErrorBoundary = Chat
// in the case of error it will receive 'error' property
export default function Chat({ error }) {
...
return <>
{error &&
<div className="toast toast-top toast-center">
<div className="alert alert-error">
...
}
<Form>
// your stuff here
</Form>
</>
} The component will be re-mounted, so instead of useState you'd want to use global state (ex. Jotai atom). With a little bit of support from Remix (like don't remount if ErrorBoundary and default component are the same) this could solve the problem |
Beta Was this translation helpful? Give feedback.
-
Any working solution to that? I'm surprised that there is no way to catch errors... Errors shouldn't propagate to the browser and should be caught by the Btw. in my case error boundary doesn't work... |
Beta Was this translation helpful? Give feedback.
-
Any updates here? [Update] Found relevant thread -> #4645 |
Beta Was this translation helpful? Give feedback.
-
How’s this still not addressed… I have to find a different way to revalidate my page on interval due to laptops sleep and wake breaking the whole page :( |
Beta Was this translation helpful? Give feedback.
-
One workaround is to wrap each route with a client loader/action. There, you can call serverLoader/serverAction inside a try-catch block. If there is an error, you could replay the last loader result and add some error decorations. Get action to work is relatively easy here. For loaders to work when the network is not ideal, you have to make sure that all parent loaders won't throw an error. |
Beta Was this translation helpful? Give feedback.
-
Doesn't look like there is any solution so far?? Did anyone find something that works?? |
Beta Was this translation helpful? Give feedback.
-
Prior to using Remix, I had a custom form component that used
fetch
to submit forms with some error handling, something like:With Remix, using either
useFetcher
oruseSubmit
, is there any way to catch these errors before they bubble up to an error boundary? The boundaries are better than nothing, but it's a pain to have the whole form disappear because of an error withfetch
instead of just showing an inline error message similar to how our validation errors appear.I know we could save a local copy of the form data before submission to try and restore them after a page reload following an error, but we have some pretty complex forms where serializing/restoring would be very cumbersome.
Beta Was this translation helpful? Give feedback.
All reactions