Harden DB-reading pages with graceful fallback (no 500 without DB)

/news and six admin pages issued raw prisma reads with no try/catch, so
a DB outage rendered a 500 instead of an empty state. Wrap each read
(try/catch or .catch(() => fallback), preserving select() row types and
notFound() on the user-detail page). Matches the fail-soft pattern used
across the rest of the app.

Verified: tsc 0, next build 0, all public pages 200 against a dead DB.
This commit is contained in:
Simo committed 2026-06-28 16:14:40 +02:00
1 parent 3e8fb794d9
commit d5efbba9f6
7 files changed
+60 -29

No files matched your search

+6 -4
View File
@@ -18,10 +18,12 @@ export default async function AdminUserDetail({
}) {
const { id } = await params;
const userId = Number(id);
const user = await prisma.user.findUnique({
where: { id: userId },
select: { id: true, username: true, motto: true, rank: true, credits: true, look: true, online: true },
});
const user = await prisma.user
.findUnique({
where: { id: userId },
select: { id: true, username: true, motto: true, rank: true, credits: true, look: true, online: true },
})
.catch(() => null);
if (!user) notFound();
return (
+7 -5
View File
@@ -15,16 +15,18 @@ export default async function AdminUsers({
const page = Math.max(1, Number(sp.page ?? "1") || 1);
const where = q ? { username: { contains: q } } : {};
const [users, total] = await Promise.all([
prisma.user.findMany({
// .catch(...) keeps the precise selected-row type while falling back to an
// empty page when the DB is unavailable (instead of a 500).
const users = await prisma.user
.findMany({
where,
select: { id: true, username: true, rank: true, credits: true, online: true },
orderBy: { id: "desc" },
skip: (page - 1) * PER_PAGE,
take: PER_PAGE,
}),
prisma.user.count({ where }),
]);
})
.catch(() => []);
const total = await prisma.user.count({ where }).catch(() => 0);
const pages = Math.max(1, Math.ceil(total / PER_PAGE));
return (