Эх сурвалжийг харах

RHL-001-refactor: enhance API error handling and logging for branches, months, days, and files

Code_Uwe 1 долоо хоног өмнө
parent
commit
9c2ca67ab1

+ 17 - 15
app/api/branches/[branch]/[year]/[month]/days/route.js

@@ -1,9 +1,10 @@
-// app/api/branches/[branch]/[year]/[month]/days/route.js
 import { NextResponse } from "next/server";
 import { listDays } from "@/lib/storage";
 
-export function GET(request, { params }) {
-	const { branch, year, month } = params;
+export async function GET(request, { params }) {
+	console.log("[/api/branches/[branch]/[year]/[month]/days] params:", params);
+
+	const { branch, year, month } = params || {};
 
 	if (!branch || !year || !month) {
 		return NextResponse.json(
@@ -12,16 +13,17 @@ export function GET(request, { params }) {
 		);
 	}
 
-	return listDays(branch, month.length === 4 ? branch : year, month)
-		.then((days) => NextResponse.json({ branch, year, month, days }))
-		.catch((error) => {
-			console.error(
-				"[api/branches/[branch]/[year]/[month]/days] Fehler:",
-				error
-			);
-			return NextResponse.json(
-				{ error: "Fehler beim Lesen der Tage" },
-				{ status: 500 }
-			);
-		});
+	try {
+		const days = await listDays(branch, year, month);
+		return NextResponse.json({ branch, year, month, days });
+	} catch (error) {
+		console.error(
+			"[/api/branches/[branch]/[year]/[month]/days] Fehler:",
+			error
+		);
+		return NextResponse.json(
+			{ error: "Fehler beim Lesen der Tage: " + error.message },
+			{ status: 500 }
+		);
+	}
 }

+ 5 - 4
app/api/branches/[branch]/[year]/months/route.js

@@ -1,9 +1,10 @@
-// app/api/branches/[branch]/[year]/months/route.js
 import { NextResponse } from "next/server";
 import { listMonths } from "@/lib/storage";
 
 export async function GET(request, { params }) {
-	const { branch, year } = params;
+	console.log("[/api/branches/[branch]/[year]/months] params:", params);
+
+	const { branch, year } = params || {};
 
 	if (!branch || !year) {
 		return NextResponse.json(
@@ -16,9 +17,9 @@ export async function GET(request, { params }) {
 		const months = await listMonths(branch, year);
 		return NextResponse.json({ branch, year, months });
 	} catch (error) {
-		console.error("[api/branches/[branch]/[year]/months] Fehler:", error);
+		console.error("[/api/branches/[branch]/[year]/months] Fehler:", error);
 		return NextResponse.json(
-			{ error: "Fehler beim Lesen der Monate" },
+			{ error: "Fehler beim Lesen der Monate: " + error.message },
 			{ status: 500 }
 		);
 	}

+ 5 - 4
app/api/branches/[branch]/years/route.js

@@ -1,9 +1,10 @@
-// app/api/branches/[branch]/years/route.js
 import { NextResponse } from "next/server";
 import { listYears } from "@/lib/storage";
 
 export async function GET(request, { params }) {
-	const { branch } = params;
+	console.log("[/api/branches/[branch]/years] params:", params);
+
+	const { branch } = params || {};
 
 	if (!branch) {
 		return NextResponse.json(
@@ -16,9 +17,9 @@ export async function GET(request, { params }) {
 		const years = await listYears(branch);
 		return NextResponse.json({ branch, years });
 	} catch (error) {
-		console.error("[api/branches/[branch]/years] Fehler:", error);
+		console.error("[/api/branches/[branch]/years] Fehler:", error);
 		return NextResponse.json(
-			{ error: "Fehler beim Lesen der Jahre" },
+			{ error: "Fehler beim Lesen der Jahre: " + error.message },
 			{ status: 500 }
 		);
 	}

+ 4 - 4
app/api/files/route.js

@@ -1,15 +1,15 @@
-// app/api/files/route.js
 import { NextResponse } from "next/server";
 import { listFiles } from "@/lib/storage";
 
 export async function GET(request) {
 	const { searchParams } = new URL(request.url);
-
 	const branch = searchParams.get("branch");
 	const year = searchParams.get("year");
 	const month = searchParams.get("month");
 	const day = searchParams.get("day");
 
+	console.log("[/api/files] query:", { branch, year, month, day });
+
 	if (!branch || !year || !month || !day) {
 		return NextResponse.json(
 			{ error: "branch, year, month, day sind erforderlich" },
@@ -21,9 +21,9 @@ export async function GET(request) {
 		const files = await listFiles(branch, year, month, day);
 		return NextResponse.json({ branch, year, month, day, files });
 	} catch (error) {
-		console.error("[api/files] Fehler:", error);
+		console.error("[/api/files] Fehler:", error);
 		return NextResponse.json(
-			{ error: "Fehler beim Lesen der Dateien" },
+			{ error: "Fehler beim Lesen der Dateien: " + error.message },
 			{ status: 500 }
 		);
 	}

+ 2 - 15
lib/storage.js

@@ -4,21 +4,13 @@ import path from "path";
 
 const ROOT = process.env.NAS_ROOT_PATH;
 
-if (!ROOT) {
-	console.warn(
-		"[storage] NAS_ROOT_PATH ist nicht gesetzt – Dateizugriff wird fehlschlagen"
-	);
-}
-
 function fullPath(...segments) {
 	if (!ROOT) {
 		throw new Error("NAS_ROOT_PATH ist nicht gesetzt");
 	}
-
 	return path.join(ROOT, ...segments.map(String));
 }
 
-// Hilfsfunktion: sortiert numerische Strings wie "1","2","10" korrekt
 function sortNumericStrings(a, b) {
 	const na = parseInt(a, 10);
 	const nb = parseInt(b, 10);
@@ -30,7 +22,6 @@ function sortNumericStrings(a, b) {
 
 export async function listBranches() {
 	const entries = await fs.readdir(fullPath(), { withFileTypes: true });
-
 	return entries
 		.filter(
 			(e) =>
@@ -47,7 +38,6 @@ export async function listBranches() {
 export async function listYears(branch) {
 	const dir = fullPath(branch);
 	const entries = await fs.readdir(dir, { withFileTypes: true });
-
 	return entries
 		.filter((e) => e.isDirectory() && /^\d{4}$/.test(e.name))
 		.map((e) => e.name)
@@ -57,7 +47,6 @@ export async function listYears(branch) {
 export async function listMonths(branch, year) {
 	const dir = fullPath(branch, year);
 	const entries = await fs.readdir(dir, { withFileTypes: true });
-
 	return entries
 		.filter((e) => e.isDirectory() && /^\d{1,2}$/.test(e.name))
 		.map((e) => e.name.padStart(2, "0"))
@@ -65,9 +54,8 @@ export async function listMonths(branch, year) {
 }
 
 export async function listDays(branch, year, month) {
-	const dir = fullPath(branch, month.length === 1 ? year : year, month);
+	const dir = fullPath(branch, year, month);
 	const entries = await fs.readdir(dir, { withFileTypes: true });
-
 	return entries
 		.filter((e) => e.isDirectory() && /^\d{1,2}$/.test(e.name))
 		.map((e) => e.name.padStart(2, "0"))
@@ -76,14 +64,13 @@ export async function listDays(branch, year, month) {
 
 export async function listFiles(branch, year, month, day) {
 	const dir = fullPath(branch, year, month, day);
-	const entries = await fs.readdir(dir, { withFileTypes: true });
+	const entries = await fs.readdir(dir);
 
 	return entries
 		.filter((name) => name.toLowerCase().endsWith(".pdf"))
 		.sort((a, b) => a.localeCompare(b, "de"))
 		.map((name) => ({
 			name,
-			// relativer Pfad, den du später für Downloads nutzen kannst
 			relativePath: `${branch}/${year}/${month}/${day}/${name}`,
 		}));
 }