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,5 +1,7 @@
import { NextResponse } from "next/server";
import { ConfigDBService } from "@/lib/services/config-db.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
export const dynamic = "force-dynamic";
@@ -15,16 +17,29 @@ export async function POST(request: Request) {
userId: mongoConfig.userId,
};
return NextResponse.json(
{ message: "Configuration sauvegardée avec succès", config },
{ message: "⚙️ Configuration sauvegardée avec succès", config },
{ status: 200 }
);
} catch (error) {
console.error("Erreur lors de la sauvegarde de la configuration:", error);
if (error instanceof Error && error.message === "Utilisateur non authentifié") {
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
return NextResponse.json(
{
error: {
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
message: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED],
},
},
{ status: 401 }
);
}
return NextResponse.json(
{ error: "Erreur lors de la sauvegarde de la configuration" },
{
error: {
code: ERROR_CODES.CONFIG.SAVE_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.CONFIG.SAVE_ERROR],
},
},
{ status: 500 }
);
}
@@ -45,14 +60,35 @@ export async function GET() {
console.error("Erreur lors de la récupération de la configuration:", error);
if (error instanceof Error) {
if (error.message === "Utilisateur non authentifié") {
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
return NextResponse.json(
{
error: {
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
message: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED],
},
},
{ status: 401 }
);
}
if (error.message === "Configuration non trouvée") {
return NextResponse.json({ error: "Configuration non trouvée" }, { status: 404 });
return NextResponse.json(
{
error: {
code: ERROR_CODES.KOMGA.MISSING_CONFIG,
message: ERROR_MESSAGES[ERROR_CODES.KOMGA.MISSING_CONFIG],
},
},
{ status: 404 }
);
}
}
return NextResponse.json(
{ error: "Erreur lors de la récupération de la configuration" },
{
error: {
code: ERROR_CODES.CONFIG.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.CONFIG.FETCH_ERROR],
},
},
{ status: 500 }
);
}