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,8 @@
import { NextResponse } from "next/server";
import { FavoriteService } from "@/lib/services/favorite.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { AppError } from "@/utils/errors";
export async function GET() {
try {
@@ -7,8 +10,24 @@ export async function GET() {
return NextResponse.json(favoriteIds);
} catch (error) {
console.error("Erreur lors de la récupération des favoris:", error);
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
code: error.code,
message: ERROR_MESSAGES[error.code],
},
},
{ status: 500 }
);
}
return NextResponse.json(
{ error: "Erreur lors de la récupération des favoris" },
{
error: {
code: ERROR_CODES.FAVORITE.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.FAVORITE.FETCH_ERROR],
},
},
{ status: 500 }
);
}
@@ -18,10 +37,29 @@ export async function POST(request: Request) {
try {
const { seriesId } = await request.json();
await FavoriteService.addToFavorites(seriesId);
return NextResponse.json({ message: "Favori ajouté avec succès" });
return NextResponse.json({ message: "⭐️ Série ajoutée aux favoris" });
} catch (error) {
console.error("Erreur lors de l'ajout du favori:", error);
return NextResponse.json({ error: "Erreur lors de l'ajout du favori" }, { status: 500 });
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
code: error.code,
message: ERROR_MESSAGES[error.code],
},
},
{ status: 500 }
);
}
return NextResponse.json(
{
error: {
code: ERROR_CODES.FAVORITE.ADD_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.FAVORITE.ADD_ERROR],
},
},
{ status: 500 }
);
}
}
@@ -29,9 +67,28 @@ export async function DELETE(request: Request) {
try {
const { seriesId } = await request.json();
await FavoriteService.removeFromFavorites(seriesId);
return NextResponse.json({ message: "Favori supprimé avec succès" });
return NextResponse.json({ message: "💔 Série retirée des favoris" });
} catch (error) {
console.error("Erreur lors de la suppression du favori:", error);
return NextResponse.json({ error: "Erreur lors de la suppression du favori" }, { status: 500 });
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
code: error.code,
message: ERROR_MESSAGES[error.code],
},
},
{ status: 500 }
);
}
return NextResponse.json(
{
error: {
code: ERROR_CODES.FAVORITE.DELETE_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.FAVORITE.DELETE_ERROR],
},
},
{ status: 500 }
);
}
}