Skip to content

Commit d33e7cc

Browse files
authored
Merge pull request #187 from azzzy/improve-error-handling
Improve error handling
2 parents b97e322 + acaea20 commit d33e7cc

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Diff for: app/routes/j/$id.tsx

+15-10
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,
@@ -62,19 +62,17 @@ export const loader: LoaderFunction = async ({ params, request }) => {
6262
});
6363

6464
if (!jsonResponse.ok) {
65-
console.log(
66-
`Failed to fetch ${doc.url}: ${jsonResponse.status} (${jsonResponse.statusText})`
67-
);
65+
const jsonResponseText = await jsonResponse.text();
66+
const error = `Failed to fetch ${doc.url}. HTTP status: ${jsonResponse.status} (${jsonResponseText}})`;
67+
console.error(error);
6868

69-
throw new Response(jsonResponse.statusText, {
69+
throw new Response(error, {
7070
status: jsonResponse.status,
7171
});
7272
}
7373

7474
const json = await jsonResponse.json();
7575

76-
console.log(`Fetched ${doc.url} with json, returning...`);
77-
7876
return {
7977
doc,
8078
json,
@@ -166,7 +164,7 @@ export const meta: MetaFunction = ({
166164
}) => {
167165
let title = "JSON Hero";
168166

169-
if (data) {
167+
if (data?.doc?.title) {
170168
title += ` - ${data.doc.title}`;
171169
}
172170

@@ -250,7 +248,10 @@ export default function JsonDocumentRoute() {
250248
}
251249

252250
export function CatchBoundary() {
251+
const error = useCatch();
253252
const params = useParams();
253+
console.log("error", error)
254+
254255
return (
255256
<div className="flex items-center justify-center w-screen h-screen bg-[rgb(56,52,139)]">
256257
<div className="w-2/3">
@@ -259,15 +260,19 @@ export function CatchBoundary() {
259260
<Logo />
260261
</div>
261262
<PageNotFoundTitle className="text-center leading-tight">
262-
404
263+
{error.status}
263264
</PageNotFoundTitle>
264265
</div>
265266
<div className="text-center leading-snug text-white">
266267
<ExtraLargeTitle className="text-slate-200 mb-8">
267268
<b>Sorry</b>! Something went wrong...
268269
</ExtraLargeTitle>
269270
<SmallSubtitle className="text-slate-200 mb-8">
270-
We couldn't find the page <b>'https://jsonhero.io/j/{params.id}</b>'
271+
{error.data || (
272+
error.status === 404
273+
? <>We couldn't find the page <b>'https://jsonhero.io/j/{params.id}'</b></>
274+
: "Unknown error occurred."
275+
)}
271276
</SmallSubtitle>
272277
<a
273278
href="/"

0 commit comments

Comments
 (0)