Skip to content

Commit 559d0d8

Browse files
Add error handling and new components
1 parent 1f42d18 commit 559d0d8

File tree

5 files changed

+87
-16
lines changed

5 files changed

+87
-16
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Link from 'next/link';
2+
import { FaceFrownIcon } from '@heroicons/react/24/outline';
3+
4+
export default function NotFound() {
5+
return (
6+
<main className="flex h-full flex-col items-center justify-center gap-2">
7+
<FaceFrownIcon className="w-10 text-gray-400" />
8+
<h2 className="text-xl font-semibold">404 Not Found</h2>
9+
<p>Could not find the requested invoice.</p>
10+
<Link
11+
href="/dashboard/invoices"
12+
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
13+
>
14+
Go Back
15+
</Link>
16+
</main>
17+
);
18+
}

app/dashboard/invoices/[id]/edit/page.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import Form from '@/app/ui/invoices/edit-form';
22
import Breadcrumbs from '@/app/ui/invoices/breadcrumbs';
33
import { fetchInvoiceById, fetchCustomers } from '@/app/lib/data';
4-
4+
import { notFound } from 'next/navigation';
5+
56
export default async function Page({ params }: { params: { id: string } }) {
67
const id = params.id;
78
const [invoice, customers] = await Promise.all([
89
fetchInvoiceById(id),
910
fetchCustomers(),
1011
]);
1112

13+
if (!invoice || !customers) {
14+
notFound();
15+
}
16+
1217
return (
1318
<main>
1419
<Breadcrumbs

app/dashboard/invoices/error.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
5+
export default function Error({
6+
error,
7+
reset,
8+
}: {
9+
error: Error & { digest?: string };
10+
reset: () => void;
11+
}) {
12+
useEffect(() => {
13+
// Optionally log the error to an error reporting service
14+
console.error(error);
15+
}, [error]);
16+
17+
return (
18+
<main className="flex h-full flex-col items-center justify-center">
19+
<h2 className="text-center">Something went wrong!</h2>
20+
<button
21+
className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
22+
onClick={
23+
// Attempt to recover by trying to re-render the invoices route
24+
() => reset()
25+
}
26+
>
27+
Try again
28+
</button>
29+
</main>
30+
);
31+
}

app/lib/actions.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ export async function createInvoice(formData: FormData) {
2424
const amountInCents = amount * 100;
2525
const date = new Date().toISOString().split('T')[0];
2626

27-
await sql`
27+
try {
28+
await sql`
2829
INSERT INTO invoices (customer_id, amount, status, date)
2930
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
3031
`;
32+
} catch (error) {
33+
return {
34+
message: 'Database Error: Failed to Create Invoice.',
35+
};
36+
}
3137

3238
revalidatePath('/dashboard/invoices');
3339
redirect('/dashboard/invoices');
@@ -43,23 +49,32 @@ export async function updateInvoice(id: string, formData: FormData) {
4349
});
4450
const amountInCents = amount * 100;
4551

46-
await sql`
52+
try {
53+
await sql`
4754
UPDATE invoices
4855
SET customer_id = ${customerId},
4956
amount = ${amountInCents},
5057
status = ${status}
5158
WHERE id = ${id}
5259
`;
60+
} catch (error) {
61+
return { message: 'Database Error: Failed to Update Invoice.' };
62+
}
5363

5464
revalidatePath('/dashboard/invoices');
5565
redirect('/dashboard/invoices');
5666
}
5767

5868
export async function deleteInvoice(id: string) {
59-
await sql`
69+
70+
try {
71+
await sql`
6072
DELETE FROM invoices
6173
WHERE id = ${id}
6274
`;
63-
64-
revalidatePath('/dashboard/invoices');
65-
}
75+
revalidatePath('/dashboard/invoices');
76+
return { message: 'Deleted Invoice.' };
77+
} catch (error) {
78+
return { message: 'Database Error: Failed to Delete Invoice.' };
79+
}
80+
}

app/lib/data.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function fetchRevenue() {
2828
return data.rows;
2929
} catch (error) {
3030
console.error('Database Error:', error);
31-
throw new Error('Failed to fetch revenue data.');
31+
// throw new Error('Failed to fetch revenue data.');
3232
}
3333
}
3434

@@ -56,7 +56,7 @@ export async function fetchLatestInvoices() {
5656
return latestInvoices;
5757
} catch (error) {
5858
console.error('Database Error:', error);
59-
throw new Error('Failed to fetch the latest invoices.');
59+
// throw new Error('Failed to fetch the latest invoices.');
6060
}
6161
}
6262

@@ -98,7 +98,7 @@ export async function fetchCardData() {
9898
};
9999
} catch (error) {
100100
console.error('Database Error:', error);
101-
throw new Error('Failed to fetch card data.');
101+
// throw new Error('Failed to fetch card data.');
102102
}
103103
}
104104

@@ -135,7 +135,7 @@ export async function fetchFilteredInvoices(
135135
return invoices.rows;
136136
} catch (error) {
137137
console.error('Database Error:', error);
138-
throw new Error('Failed to fetch invoices.');
138+
// throw new Error('Failed to fetch invoices.');
139139
}
140140
}
141141

@@ -158,7 +158,7 @@ export async function fetchInvoicesPages(query: string) {
158158
return totalPages;
159159
} catch (error) {
160160
console.error('Database Error:', error);
161-
throw new Error('Failed to fetch total number of invoices.');
161+
// throw new Error('Failed to fetch total number of invoices.');
162162
}
163163
}
164164

@@ -182,10 +182,12 @@ export async function fetchInvoiceById(id: string) {
182182
amount: invoice.amount / 100,
183183
}));
184184

185+
console.log(invoice)
186+
185187
return invoice[0];
186188
} catch (error) {
187189
console.error('Database Error:', error);
188-
throw new Error('Failed to fetch invoice.');
190+
// throw new Error('Failed to fetch invoice.');
189191
}
190192
}
191193

@@ -203,7 +205,7 @@ export async function fetchCustomers() {
203205
return customers;
204206
} catch (err) {
205207
console.error('Database Error:', err);
206-
throw new Error('Failed to fetch all customers.');
208+
// throw new Error('Failed to fetch all customers.');
207209
}
208210
}
209211

@@ -236,7 +238,7 @@ export async function fetchFilteredCustomers(query: string) {
236238
return customers;
237239
} catch (err) {
238240
console.error('Database Error:', err);
239-
throw new Error('Failed to fetch customer table.');
241+
// throw new Error('Failed to fetch customer table.');
240242
}
241243
}
242244

@@ -246,6 +248,6 @@ export async function getUser(email: string) {
246248
return user.rows[0] as User;
247249
} catch (error) {
248250
console.error('Failed to fetch user:', error);
249-
throw new Error('Failed to fetch user.');
251+
// throw new Error('Failed to fetch user.');
250252
}
251253
}

0 commit comments

Comments
 (0)