Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #187

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/routes/j/$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
LoaderFunction,
MetaFunction,
Outlet,
redirect,
redirect, ThrownResponse, useCatch,
useLoaderData,
useLocation,
useParams,
Expand Down Expand Up @@ -57,19 +57,17 @@ export const loader: LoaderFunction = async ({ params, request }) => {
const jsonResponse = await safeFetch(doc.url);

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

throw new Response(jsonResponse.statusText, {
throw new Response(error, {
status: jsonResponse.status,
});
}

const json = await jsonResponse.json();

console.log(`Fetched ${doc.url} with json, returning...`);

return {
doc,
json,
Expand Down Expand Up @@ -161,7 +159,7 @@ export const meta: MetaFunction = ({
}) => {
let title = "JSON Hero";

if (data) {
if (data?.doc?.title) {
title += ` - ${data.doc.title}`;
}

Expand Down Expand Up @@ -245,7 +243,10 @@ export default function JsonDocumentRoute() {
}

export function CatchBoundary() {
const error = useCatch();
const params = useParams();
console.log("error", error)

return (
<div className="flex items-center justify-center w-screen h-screen bg-[rgb(56,52,139)]">
<div className="w-2/3">
Expand All @@ -254,15 +255,19 @@ export function CatchBoundary() {
<Logo />
</div>
<PageNotFoundTitle className="text-center leading-tight">
404
{error.status}
</PageNotFoundTitle>
</div>
<div className="text-center leading-snug text-white">
<ExtraLargeTitle className="text-slate-200 mb-8">
<b>Sorry</b>! Something went wrong...
</ExtraLargeTitle>
<SmallSubtitle className="text-slate-200 mb-8">
We couldn't find the page <b>'https://jsonhero.io/j/{params.id}</b>'
{error.data || (
error.status === 404
? <>We couldn't find the page <b>'https://jsonhero.io/j/{params.id}'</b></>
: "Unknown error occurred."
)}
</SmallSubtitle>
<a
href="/"
Expand Down