Chapter 13 - Handle generic errors
This commit is contained in:
31
app/dashboard/invoices/error.tsx
Normal file
31
app/dashboard/invoices/error.tsx
Normal 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>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -28,10 +28,16 @@ export async function createInvoice(formData: FormData) {
|
|||||||
const amountInCents = amount * 100;
|
const amountInCents = amount * 100;
|
||||||
const date = new Date().toISOString().split('T')[0];
|
const date = new Date().toISOString().split('T')[0];
|
||||||
|
|
||||||
|
try {
|
||||||
await sql`
|
await sql`
|
||||||
INSERT INTO invoices (customer_id, amount, status, date)
|
INSERT INTO invoices (customer_id, amount, status, date)
|
||||||
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
|
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
|
||||||
`;
|
`;
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
message: 'Database Error: Failed to Create Invoice.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
revalidatePath('/dashboard/invoices');
|
revalidatePath('/dashboard/invoices');
|
||||||
redirect('/dashboard/invoices');
|
redirect('/dashboard/invoices');
|
||||||
@@ -47,17 +53,27 @@ export async function updateInvoice(id: string, formData: FormData) {
|
|||||||
|
|
||||||
const amountInCents = amount * 100;
|
const amountInCents = amount * 100;
|
||||||
|
|
||||||
|
try {
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE invoices
|
UPDATE invoices
|
||||||
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
|
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
|
||||||
WHERE id = ${id}
|
WHERE id = ${id}
|
||||||
`;
|
`;
|
||||||
|
} catch (error) {
|
||||||
|
return { message: 'Database Error: Failed to Update Invoice.' };
|
||||||
|
}
|
||||||
|
|
||||||
revalidatePath('/dashboard/invoices');
|
revalidatePath('/dashboard/invoices');
|
||||||
redirect('/dashboard/invoices');
|
redirect('/dashboard/invoices');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteInvoice(id: string) {
|
export async function deleteInvoice(id: string) {
|
||||||
|
throw new Error('Failed to Delete Invoice');
|
||||||
|
try {
|
||||||
await sql`DELETE FROM invoices WHERE id = ${id}`;
|
await sql`DELETE FROM invoices WHERE id = ${id}`;
|
||||||
revalidatePath('/dashboard/invoices');
|
revalidatePath('/dashboard/invoices');
|
||||||
|
return { message: 'Deleted Invoice.' };
|
||||||
|
} catch (error) {
|
||||||
|
return { message: 'Database Error: Failed to Delete Invoice.' };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user