diff --git a/app/dashboard/invoices/error.tsx b/app/dashboard/invoices/error.tsx new file mode 100644 index 0000000..c83b80d --- /dev/null +++ b/app/dashboard/invoices/error.tsx @@ -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 ( +
+

Something went wrong!

+ +
+ ); +} \ No newline at end of file diff --git a/app/lib/actions.ts b/app/lib/actions.ts index 88cbcdf..27f2a16 100644 --- a/app/lib/actions.ts +++ b/app/lib/actions.ts @@ -28,10 +28,16 @@ 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) { + return { + message: 'Database Error: Failed to Create Invoice.', + }; + } revalidatePath('/dashboard/invoices'); redirect('/dashboard/invoices'); @@ -47,17 +53,27 @@ export async function updateInvoice(id: string, formData: FormData) { const amountInCents = amount * 100; - await sql` - UPDATE invoices - SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status} - WHERE id = ${id} - `; + try { + await sql` + UPDATE invoices + SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status} + WHERE id = ${id} + `; + } catch (error) { + return { message: 'Database Error: Failed to Update Invoice.' }; + } revalidatePath('/dashboard/invoices'); redirect('/dashboard/invoices'); } export async function deleteInvoice(id: string) { - await sql`DELETE FROM invoices WHERE id = ${id}`; - revalidatePath('/dashboard/invoices'); + throw new Error('Failed to Delete Invoice'); + try { + await sql`DELETE FROM invoices WHERE id = ${id}`; + revalidatePath('/dashboard/invoices'); + return { message: 'Deleted Invoice.' }; + } catch (error) { + return { message: 'Database Error: Failed to Delete Invoice.' }; + } } \ No newline at end of file