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
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import { withRetry } from "@/lib/prisma-retry";
|
|
import { prisma } from "@/lib";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
const { id } = params;
|
|
console.log("User ID:", id);
|
|
|
|
try {
|
|
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);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
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",
|
|
});
|
|
}
|
|
}
|