implement error handling

This commit is contained in:
Michi 2025-04-21 11:27:08 +02:00
parent 39005d46a5
commit 7d2dea886c
5 changed files with 144 additions and 8 deletions

View file

@ -0,0 +1,18 @@
import Link from 'next/link';
import { FaceFrownIcon } from '@heroicons/react/24/outline';
export default function NotFound() {
return (
<main className="flex h-full flex-col items-center justify-center gap-2">
<FaceFrownIcon className="w-10 text-gray-400" />
<h2 className="text-xl font-semibold">404 Not Found</h2>
<p>Could not find the requested invoice.</p>
<Link
href="/dashboard/invoices"
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
>
Go Back
</Link>
</main>
);
}

View file

@ -1,6 +1,7 @@
import Form from '@/app/ui/invoices/edit-form';
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
import { fetchCustomers, fetchInvoiceById } from '@/app/lib/data';
import { notFound } from 'next/navigation';
export default async function Page(props: {
params: Promise<{
@ -14,6 +15,10 @@ export default async function Page(props: {
fetchCustomers(),
]);
if(!invoice){
notFound();
}
return (
<main>
<Breadcrumbs

View file

@ -0,0 +1,31 @@
'use client';
import { useEffect } from 'react';
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
useEffect(() => {
// Optionally log the error to an error reporting service
console.error(error);
}, [error]);
return (
<main className="flex h-full flex-col items-center justify-center">
<h2 className="text-center">Something went wrong!</h2>
<button
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
onClick={
// Attempt to recover by trying to re-render the invoices route
() => reset()
}
>
Try again
</button>
</main>
);
}

View file

@ -27,10 +27,14 @@ export async function createInvoice(formData: FormData){
const amountInCents = amount * 100;
const date = new Date().toISOString().split('T')[0];
await sql`
INSERT INTO invoices (customer_Id, amount, status, date)
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
`;
try{
await sql`
INSERT INTO invoices (customer_Id, amount, status, date)
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
`;
} catch(error){
console.error(error);
}
revalidatePath('/dashboard/invoices');
redirect('/dashboard/invoices');
@ -46,17 +50,27 @@ export async function updateInvoice(id: string, formData: FormData){
});
const amountInCents = amount * 100;
await sql`
try{
await sql`
UPDATE invoices
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
WHERE id = ${id}
`;
`;
} catch(error){
console.error(error);
}
revalidatePath('/dashboard/invoices');
redirect('/dashboard/invoices');
}
export async function deleteInvoice(id: string){
await sql`DELETE FROM invoices WHERE id = ${id}`;
export async function deleteInvoice(id: string){
try{
await sql`DELETE FROM invoices WHERE id = ${id}`;
} catch (error){
console.error(error);
}
revalidatePath('/dashboard/invoices');
}