Chapter 13 - Handle generic errors
This commit is contained in:
@@ -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.' };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user