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 { NextRequest, NextResponse } from "next/server";
import { BookService } from "@/lib/services/book.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { AppError } from "@/utils/errors";
export const dynamic = "force-dynamic";
@@ -12,6 +15,25 @@ export async function GET(
return response;
} catch (error) {
console.error("Erreur lors de la récupération de la page du livre:", error);
return new NextResponse("Erreur lors de la récupération de la page", { 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.IMAGE.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
},
},
{ status: 500 }
);
}
}

View File

@@ -1,28 +1,52 @@
import { NextResponse } from "next/server";
import { NextRequest, NextResponse } from "next/server";
import { BookService } from "@/lib/services/book.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { AppError } from "@/utils/errors";
export const dynamic = "force-dynamic";
export async function GET(
request: Request,
request: NextRequest,
{ params }: { params: { bookId: string; pageNumber: string } }
) {
try {
// Convertir le numéro de page en nombre
const pageNumber = parseInt(params.pageNumber);
if (isNaN(pageNumber) || pageNumber < 0) {
return NextResponse.json({ error: "Numéro de page invalide" }, { status: 400 });
return NextResponse.json(
{
error: {
code: ERROR_CODES.BOOK.PAGES_FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.BOOK.PAGES_FETCH_ERROR],
},
},
{ status: 400 }
);
}
const response = await BookService.getPageThumbnail(params.bookId, pageNumber);
return response;
} catch (error) {
console.error("API Book Page Thumbnail - Erreur:", error);
if (error instanceof Error) {
return NextResponse.json({ error: error.message }, { status: 500 });
console.error("Erreur lors de la récupération de la miniature de la page:", error);
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
code: error.code,
message: ERROR_MESSAGES[error.code],
},
},
{ status: 500 }
);
}
return NextResponse.json(
{ error: "Une erreur est survenue lors de la récupération de la miniature" },
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
},
},
{ status: 500 }
);
}

View File

@@ -1,5 +1,8 @@
import { NextRequest, NextResponse } from "next/server";
import { BookService } from "@/lib/services/book.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { AppError } from "@/utils/errors";
export async function GET(request: NextRequest, { params }: { params: { bookId: string } }) {
try {
@@ -7,6 +10,25 @@ export async function GET(request: NextRequest, { params }: { params: { bookId:
return response;
} catch (error) {
console.error("Erreur lors de la récupération de la miniature du livre:", error);
return new NextResponse("Erreur lors de la récupération de la miniature", { 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.IMAGE.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
},
},
{ status: 500 }
);
}
}

View File

@@ -1,5 +1,8 @@
import { NextRequest, NextResponse } from "next/server";
import { SeriesService } from "@/lib/services/series.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { AppError } from "@/utils/errors";
export const dynamic = "force-dynamic";
@@ -9,6 +12,25 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId
return response;
} catch (error) {
console.error("Erreur lors de la récupération de la couverture de la série:", error);
return new NextResponse("Erreur lors de la récupération de la couverture", { 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.IMAGE.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
},
},
{ status: 500 }
);
}
}

View File

@@ -1,5 +1,8 @@
import { NextRequest, NextResponse } from "next/server";
import { SeriesService } from "@/lib/services/series.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { AppError } from "@/utils/errors";
export async function GET(request: NextRequest, { params }: { params: { seriesId: string } }) {
try {
@@ -7,6 +10,25 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId
return response;
} catch (error) {
console.error("Erreur lors de la récupération de la miniature de la série:", error);
return new NextResponse("Erreur lors de la récupération de la miniature", { 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.IMAGE.FETCH_ERROR,
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
},
},
{ status: 500 }
);
}
}