I have a component structured like this with my ErrorBoundary
wrapping my Suspense
element.
function App() {
return (
<ErrorBoundary fallback={<h2>Could not fetch cities.</h2>}>
<Suspense fallback={<div>Loading..</div>}>
<MyList />
</Suspense>
</ErrorBoundary>
);
}
The MyList
component includes an SWR
data fetching hook as follows:
const { data } = useSwr(`/api/mydata`, fetcher, {
suspense: true,
});
My fetcher method throws an error as follows:
const rsp = await fetch(url);
if (rsp.ok) {
return await rsp.json();
} else {
const MyError = function (message, status) {
this.message = `${message} from url ${url} status code:${status}`;
this.status = status;
};
throw new MyError(rsp.statusText, rsp.status);
}
}
When the error happens, I don't know how to have my UI show the values thrown (that is, what is in the MyError class)