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 async function GET() {
try {
@@ -9,11 +11,24 @@ export async function GET() {
console.error("Erreur lors de la récupération de la configuration TTL:", 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 }
);
}
}
return NextResponse.json(
{ error: "Erreur lors de la récupération de la configuration TTL" },
{
error: {
code: ERROR_CODES.CONFIG.TTL_FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.CONFIG.TTL_FETCH_ERROR],
},
},
{ status: 500 }
);
}
@@ -24,7 +39,7 @@ export async function POST(request: Request) {
const data = await request.json();
const config = await ConfigDBService.saveTTLConfig(data);
return NextResponse.json({
message: "Configuration TTL sauvegardée avec succès",
message: "⏱️ Configuration TTL sauvegardée avec succès",
config: {
defaultTTL: config.defaultTTL,
homeTTL: config.homeTTL,
@@ -37,10 +52,23 @@ export async function POST(request: Request) {
} catch (error) {
console.error("Erreur lors de la sauvegarde de la configuration TTL:", 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 TTL" },
{
error: {
code: ERROR_CODES.CONFIG.TTL_SAVE_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.CONFIG.TTL_SAVE_ERROR],
},
},
{ status: 500 }
);
}