refacto: errors in apis

This commit is contained in:
Julien Froidefond
2025-02-25 08:40:06 +01:00
parent bf6fa0a71d
commit a690a5af6f
29 changed files with 720 additions and 109 deletions

View File

@@ -1,9 +1,24 @@
import { NextResponse } from "next/server";
import { getServerCacheService } from "@/lib/services/server-cache.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
export async function GET() {
const cacheService = await getServerCacheService();
return NextResponse.json({ mode: cacheService.getCacheMode() });
try {
const cacheService = await getServerCacheService();
return NextResponse.json({ mode: cacheService.getCacheMode() });
} catch (error) {
console.error("Erreur lors de la récupération du mode de cache:", error);
return NextResponse.json(
{
error: {
code: ERROR_CODES.CACHE.MODE_FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.CACHE.MODE_FETCH_ERROR],
},
},
{ status: 500 }
);
}
}
export async function POST(request: Request) {
@@ -11,7 +26,12 @@ export async function POST(request: Request) {
const { mode } = await request.json();
if (mode !== "file" && mode !== "memory") {
return NextResponse.json(
{ error: "Invalid mode. Must be 'file' or 'memory'" },
{
error: {
code: ERROR_CODES.CACHE.INVALID_MODE,
message: ERROR_MESSAGES[ERROR_CODES.CACHE.INVALID_MODE],
},
},
{ status: 400 }
);
}
@@ -21,6 +41,14 @@ export async function POST(request: Request) {
return NextResponse.json({ mode: cacheService.getCacheMode() });
} catch (error) {
console.error("Erreur lors de la mise à jour du mode de cache:", error);
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
return NextResponse.json(
{
error: {
code: ERROR_CODES.CACHE.MODE_UPDATE_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.CACHE.MODE_UPDATE_ERROR],
},
},
{ status: 500 }
);
}
}