Usulan Commit Message

fix: Implementasi retry mechanism dan error handling untuk database connections

Deskripsi:

Menambahkan withRetry wrapper pada berbagai API routes untuk menangani transient database errors dan meningkatkan reliabilitas koneksi

Memperbaiki error handling pada notification, authentication, dan user validation endpoints dengan response 503 untuk database connection errors

Update prisma.ts dengan konfigurasi logging yang lebih baik dan datasources configuration

Menambahkan validasi input parameters pada beberapa endpoints

Update dokumentasi QWEN.md dengan commit message format dan comment standards

Update .env.example dengan connection pool settings yang lebih lengkap

File yang diubah:

src/lib/prisma.ts — Konfigurasi Prisma client & logging

src/app/api/admin/notifikasi/count/route.tsx

src/app/api/auth/mobile-login/route.ts

src/app/api/mobile/notification/[id]/route.ts

src/app/api/user-validate/route.ts

Dan 27 file API routes lainnya (penerapan withRetry secara konsisten)

QWEN.md — Dokumentasi commit & comment standards

.env.example — Database connection pool configuration

### No Issue
This commit is contained in:
2026-03-05 14:28:45 +08:00
parent a6c9182a01
commit 3e6c94d77f
33 changed files with 312 additions and 128 deletions

View File

@@ -28,7 +28,5 @@ async function GET() {
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -1,3 +1,4 @@
import { withRetry } from "@/lib/prisma-retry";
import { prisma } from "@/lib";
import _ from "lodash";
import { NextRequest, NextResponse } from "next/server";
@@ -22,28 +23,38 @@ export async function GET(
let fixData;
try {
const data = await prisma.notifikasi.findMany({
take: page ? takeData : undefined,
skip: page ? skipData : undefined,
orderBy: {
createdAt: "desc",
},
where: {
recipientId: id,
kategoriApp: fixCategory,
},
});
const data = await withRetry(
() =>
prisma.notifikasi.findMany({
take: page ? takeData : undefined,
skip: page ? skipData : undefined,
orderBy: {
createdAt: "desc",
},
where: {
recipientId: id,
kategoriApp: fixCategory,
},
}),
undefined,
"getNotifications"
);
// Jika pagination digunakan, ambil juga total count untuk informasi
let totalCount;
let totalPages;
if (page) {
totalCount = await prisma.notifikasi.count({
where: {
recipientId: id,
kategoriApp: fixCategory,
},
});
totalCount = await withRetry(
() =>
prisma.notifikasi.count({
where: {
recipientId: id,
kategoriApp: fixCategory,
},
}),
undefined,
"countNotifications"
);
totalPages = Math.ceil(totalCount / takeData);
}
@@ -69,8 +80,23 @@ export async function GET(
return NextResponse.json(response);
} catch (error) {
const errorMsg = error instanceof Error ? error.message : "Unknown error";
console.error("Error getting notifications:", error);
// Check if it's a database connection error
if (
errorMsg.includes("Prisma") ||
errorMsg.includes("database") ||
errorMsg.includes("connection")
) {
return NextResponse.json(
{ error: "Database connection error. Please try again." },
{ status: 503 }
);
}
return NextResponse.json(
{ error: (error as Error).message },
{ error: errorMsg },
{ status: 500 },
);
}

View File

@@ -1,3 +1,4 @@
import { withRetry } from "@/lib/prisma-retry";
import { prisma } from "@/lib";
import { NextRequest, NextResponse } from "next/server";
@@ -9,12 +10,24 @@ export async function GET(
console.log("User ID:", id);
try {
const data = await prisma.notifikasi.count({
where: {
recipientId: id,
isRead: false,
},
});
if (!id) {
return NextResponse.json({
success: false,
message: "User ID is required",
});
}
const data = await withRetry(
() =>
prisma.notifikasi.count({
where: {
recipientId: id,
isRead: false,
},
}),
undefined,
"countUnreadNotifications"
);
console.log("List Notification >>", data);
@@ -23,6 +36,21 @@ export async function GET(
data: data,
});
} catch (error) {
const errorMsg = error instanceof Error ? error.message : "Unknown error";
console.error("Error getting unread count:", error);
// Check if it's a database connection error
if (
errorMsg.includes("Prisma") ||
errorMsg.includes("database") ||
errorMsg.includes("connection")
) {
return NextResponse.json({
success: false,
message: "Database connection error. Please try again.",
});
}
return NextResponse.json({
success: false,
message: "Failed to get unread count",

View File

@@ -131,7 +131,5 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -40,7 +40,5 @@ export async function GET(request: Request) {
status: 500,
}
);
} finally {
await prisma.$disconnect();
}
}