refacto: errors in apis
This commit is contained in:
@@ -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 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user