Skip to content

Commit acaea20

Browse files
committed
fix: show the fetch errors when thrown, instead of hardcoded 404
1 parent ebdeb78 commit acaea20

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

app/routes/j/$id.tsx

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
LoaderFunction,
44
MetaFunction,
55
Outlet,
6-
redirect,
6+
redirect, ThrownResponse, useCatch,
77
useLoaderData,
88
useLocation,
99
useParams,
@@ -57,19 +57,17 @@ export const loader: LoaderFunction = async ({ params, request }) => {
5757
const jsonResponse = await safeFetch(doc.url);
5858

5959
if (!jsonResponse.ok) {
60-
console.log(
61-
`Failed to fetch ${doc.url}: ${jsonResponse.status} (${jsonResponse.statusText})`
62-
);
60+
const jsonResponseText = await jsonResponse.text();
61+
const error = `Failed to fetch ${doc.url}. HTTP status: ${jsonResponse.status} (${jsonResponseText}})`;
62+
console.error(error);
6363

64-
throw new Response(jsonResponse.statusText, {
64+
throw new Response(error, {
6565
status: jsonResponse.status,
6666
});
6767
}
6868

6969
const json = await jsonResponse.json();
7070

71-
console.log(`Fetched ${doc.url} with json, returning...`);
72-
7371
return {
7472
doc,
7573
json,
@@ -245,7 +243,10 @@ export default function JsonDocumentRoute() {
245243
}
246244

247245
export function CatchBoundary() {
246+
const error = useCatch();
248247
const params = useParams();
248+
console.log("error", error)
249+
249250
return (
250251
<div className="flex items-center justify-center w-screen h-screen bg-[rgb(56,52,139)]">
251252
<div className="w-2/3">
@@ -254,15 +255,19 @@ export function CatchBoundary() {
254255
<Logo />
255256
</div>
256257
<PageNotFoundTitle className="text-center leading-tight">
257-
404
258+
{error.status}
258259
</PageNotFoundTitle>
259260
</div>
260261
<div className="text-center leading-snug text-white">
261262
<ExtraLargeTitle className="text-slate-200 mb-8">
262263
<b>Sorry</b>! Something went wrong...
263264
</ExtraLargeTitle>
264265
<SmallSubtitle className="text-slate-200 mb-8">
265-
We couldn't find the page <b>'https://jsonhero.io/j/{params.id}</b>'
266+
{error.data || (
267+
error.status === 404
268+
? <>We couldn't find the page <b>'https://jsonhero.io/j/{params.id}'</b></>
269+
: "Unknown error occurred."
270+
)}
266271
</SmallSubtitle>
267272
<a
268273
href="/"

0 commit comments

Comments
 (0)