refacto: errors in apis
This commit is contained in:
@@ -1,14 +1,30 @@
|
||||
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 {
|
||||
const response = await BookService.getPage(params.bookId, parseInt(params.pageNumber));
|
||||
const pageNumber = parseInt(params.pageNumber);
|
||||
if (isNaN(pageNumber) || pageNumber < 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.IMAGE.FETCH_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const response = await BookService.getPage(params.bookId, pageNumber);
|
||||
const buffer = await response.arrayBuffer();
|
||||
const headers = new Headers();
|
||||
headers.set("Content-Type", response.headers.get("Content-Type") || "image/jpeg");
|
||||
@@ -19,9 +35,25 @@ export async function GET(
|
||||
headers,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("API Book Page - Erreur:", error);
|
||||
console.error("Erreur lors de la récupération 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: "Erreur lors de la récupération de la page" },
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.IMAGE.FETCH_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,35 +1,76 @@
|
||||
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 async function PATCH(request: Request, { params }: { params: { bookId: string } }) {
|
||||
export async function PATCH(request: NextRequest, { params }: { params: { bookId: string } }) {
|
||||
try {
|
||||
const { page, completed } = await request.json();
|
||||
|
||||
if (typeof page !== "number") {
|
||||
return NextResponse.json(
|
||||
{ error: "Le numéro de page est requis et doit être un nombre" },
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await BookService.updateReadProgress(params.bookId, page, completed);
|
||||
return NextResponse.json({ message: "Progression mise à jour avec succès" });
|
||||
return NextResponse.json({ message: "📖 Progression mise à jour avec succès" });
|
||||
} catch (error) {
|
||||
console.error("API Read Progress - Erreur:", error);
|
||||
console.error("Erreur lors de la mise à jour de la progression:", 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 mise à jour de la progression" },
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
export async function DELETE(request: Request, { params }: { params: { bookId: string } }) {
|
||||
|
||||
export async function DELETE(request: NextRequest, { params }: { params: { bookId: string } }) {
|
||||
try {
|
||||
await BookService.deleteReadProgress(params.bookId);
|
||||
return NextResponse.json({ message: "Progression supprimée avec succès" });
|
||||
return NextResponse.json({ message: "🗑️ Progression supprimée avec succès" });
|
||||
} catch (error) {
|
||||
console.error("API Delete Read Progress - Erreur:", error);
|
||||
console.error("Erreur lors de la suppression de la progression:", 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 suppression de la progression" },
|
||||
{
|
||||
error: {
|
||||
code: ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { 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: Request, { params }: { params: { bookId: string } }) {
|
||||
try {
|
||||
@@ -7,6 +10,25 @@ export async function GET(request: Request, { params }: { params: { bookId: stri
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("API Books - Erreur:", error);
|
||||
return NextResponse.json({ error: "Erreur lors de la récupération du tome" }, { 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.BOOK.NOT_FOUND,
|
||||
message: ERROR_MESSAGES[ERROR_CODES.BOOK.NOT_FOUND],
|
||||
},
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user