From 246c0c650a31c5baf75703f8b67069cbe0464a35 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Thu, 27 Feb 2025 14:26:48 +0100 Subject: [PATCH] refacto: massive use of getMessageError --- src/app/api/auth/login/route.ts | 6 +++--- src/app/api/auth/logout/route.ts | 4 ++-- src/app/api/auth/register/route.ts | 6 +++--- src/app/api/debug/route.ts | 14 +++++++------- .../books/[bookId]/pages/[pageNumber]/route.ts | 8 ++++---- .../komga/books/[bookId]/read-progress/route.ts | 12 ++++++------ src/app/api/komga/books/[bookId]/route.ts | 6 +++--- src/app/api/komga/cache/clear/route.ts | 4 ++-- src/app/api/komga/cache/mode/route.ts | 8 ++++---- src/app/api/komga/config/route.ts | 12 ++++++------ src/app/api/komga/favorites/route.ts | 14 +++++++------- .../books/[bookId]/pages/[pageNumber]/route.ts | 6 +++--- .../pages/[pageNumber]/thumbnail/route.ts | 8 ++++---- .../images/books/[bookId]/thumbnail/route.ts | 6 +++--- .../images/series/[seriesId]/first-page/route.ts | 6 +++--- .../images/series/[seriesId]/thumbnail/route.ts | 6 +++--- src/app/api/komga/libraries/route.ts | 6 +++--- src/app/api/komga/series/[seriesId]/route.ts | 6 +++--- src/app/api/komga/test/route.ts | 4 ++-- src/app/api/komga/ttl-config/route.ts | 10 +++++----- src/app/api/preferences/route.ts | 10 +++++----- src/components/auth/RegisterForm.tsx | 4 ++-- src/components/layout/Sidebar.tsx | 10 ++++++---- src/components/series/SeriesHeader.tsx | 6 +++--- src/lib/services/auth.service.ts | 8 ++++---- src/middleware.ts | 15 ++++++++------- 26 files changed, 104 insertions(+), 101 deletions(-) diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts index b95c9cf..cce931b 100644 --- a/src/app/api/auth/login/route.ts +++ b/src/app/api/auth/login/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { AuthServerService } from "@/lib/services/auth-server.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; import { AppError } from "@/utils/errors"; import { UserData } from "@/lib/services/auth-server.service"; @@ -23,7 +23,7 @@ export async function POST(request: Request) { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 401 } @@ -37,7 +37,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.AUTH.INVALID_CREDENTIALS, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.INVALID_CREDENTIALS], + message: getErrorMessage(ERROR_CODES.AUTH.INVALID_CREDENTIALS), }, }, { status: 500 } diff --git a/src/app/api/auth/logout/route.ts b/src/app/api/auth/logout/route.ts index 986dd7a..805f604 100644 --- a/src/app/api/auth/logout/route.ts +++ b/src/app/api/auth/logout/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { cookies } from "next/headers"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; export async function POST() { try { @@ -14,7 +14,7 @@ export async function POST() { { error: { code: ERROR_CODES.AUTH.LOGOUT_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.LOGOUT_ERROR], + message: getErrorMessage(ERROR_CODES.AUTH.LOGOUT_ERROR), }, }, { status: 500 } diff --git a/src/app/api/auth/register/route.ts b/src/app/api/auth/register/route.ts index 59ae447..06a8cc5 100644 --- a/src/app/api/auth/register/route.ts +++ b/src/app/api/auth/register/route.ts @@ -1,8 +1,8 @@ import { NextResponse } from "next/server"; import { AuthServerService, UserData } from "@/lib/services/auth-server.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; import { AppError } from "@/utils/errors"; +import { getErrorMessage } from "@/utils/errors"; export async function POST(request: Request) { try { @@ -27,7 +27,7 @@ export async function POST(request: Request) { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status } @@ -41,7 +41,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.AUTH.INVALID_USER_DATA, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.INVALID_USER_DATA], + message: getErrorMessage(ERROR_CODES.AUTH.INVALID_USER_DATA), }, }, { status: 500 } diff --git a/src/app/api/debug/route.ts b/src/app/api/debug/route.ts index feb431f..4e307b9 100644 --- a/src/app/api/debug/route.ts +++ b/src/app/api/debug/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import { DebugService, RequestTiming } from "@/lib/services/debug.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; import { AppError } from "@/utils/errors"; export async function GET() { @@ -15,7 +15,7 @@ export async function GET() { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -25,7 +25,7 @@ export async function GET() { { error: { code: ERROR_CODES.DEBUG.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.DEBUG.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.DEBUG.FETCH_ERROR), }, }, { status: 500 } @@ -47,7 +47,7 @@ export async function POST(request: NextRequest) { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -57,7 +57,7 @@ export async function POST(request: NextRequest) { { error: { code: ERROR_CODES.DEBUG.SAVE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.DEBUG.SAVE_ERROR], + message: getErrorMessage(ERROR_CODES.DEBUG.SAVE_ERROR), }, }, { status: 500 } @@ -78,7 +78,7 @@ export async function DELETE() { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -88,7 +88,7 @@ export async function DELETE() { { error: { code: ERROR_CODES.DEBUG.CLEAR_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.DEBUG.CLEAR_ERROR], + message: getErrorMessage(ERROR_CODES.DEBUG.CLEAR_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/books/[bookId]/pages/[pageNumber]/route.ts b/src/app/api/komga/books/[bookId]/pages/[pageNumber]/route.ts index ec8c3ce..d6d5d0a 100644 --- a/src/app/api/komga/books/[bookId]/pages/[pageNumber]/route.ts +++ b/src/app/api/komga/books/[bookId]/pages/[pageNumber]/route.ts @@ -1,7 +1,7 @@ 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 { getErrorMessage } from "@/utils/errors"; import { AppError } from "@/utils/errors"; export const dynamic = "force-dynamic"; @@ -17,7 +17,7 @@ export async function GET( { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 400 } @@ -41,7 +41,7 @@ export async function GET( { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -51,7 +51,7 @@ export async function GET( { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/books/[bookId]/read-progress/route.ts b/src/app/api/komga/books/[bookId]/read-progress/route.ts index 4bf71dc..0044142 100644 --- a/src/app/api/komga/books/[bookId]/read-progress/route.ts +++ b/src/app/api/komga/books/[bookId]/read-progress/route.ts @@ -1,7 +1,7 @@ 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 { getErrorMessage } from "@/utils/errors"; import { AppError } from "@/utils/errors"; export async function PATCH(request: NextRequest, { params }: { params: { bookId: string } }) { @@ -13,7 +13,7 @@ export async function PATCH(request: NextRequest, { params }: { params: { bookId { error: { code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR], + message: getErrorMessage(ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR), }, }, { status: 400 } @@ -29,7 +29,7 @@ export async function PATCH(request: NextRequest, { params }: { params: { bookId { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -39,7 +39,7 @@ export async function PATCH(request: NextRequest, { params }: { params: { bookId { error: { code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR], + message: getErrorMessage(ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR), }, }, { status: 500 } @@ -58,7 +58,7 @@ export async function DELETE(request: NextRequest, { params }: { params: { bookI { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -68,7 +68,7 @@ export async function DELETE(request: NextRequest, { params }: { params: { bookI { error: { code: ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR], + message: getErrorMessage(ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/books/[bookId]/route.ts b/src/app/api/komga/books/[bookId]/route.ts index ae4276a..4074821 100644 --- a/src/app/api/komga/books/[bookId]/route.ts +++ b/src/app/api/komga/books/[bookId]/route.ts @@ -1,7 +1,7 @@ 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 { getErrorMessage } from "@/utils/errors"; import { AppError } from "@/utils/errors"; import { KomgaBookWithPages } from "@/types/komga"; export async function GET(request: Request, { params }: { params: { bookId: string } }) { @@ -15,7 +15,7 @@ export async function GET(request: Request, { params }: { params: { bookId: stri { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -25,7 +25,7 @@ export async function GET(request: Request, { params }: { params: { bookId: stri { error: { code: ERROR_CODES.BOOK.NOT_FOUND, - message: ERROR_MESSAGES[ERROR_CODES.BOOK.NOT_FOUND], + message: getErrorMessage(ERROR_CODES.BOOK.NOT_FOUND), }, }, { status: 500 } diff --git a/src/app/api/komga/cache/clear/route.ts b/src/app/api/komga/cache/clear/route.ts index fa62b09..05ccb14 100644 --- a/src/app/api/komga/cache/clear/route.ts +++ b/src/app/api/komga/cache/clear/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { getServerCacheService, ServerCacheService } from "@/lib/services/server-cache.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; export async function POST() { try { @@ -14,7 +14,7 @@ export async function POST() { { error: { code: ERROR_CODES.CACHE.CLEAR_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CACHE.CLEAR_ERROR], + message: getErrorMessage(ERROR_CODES.CACHE.CLEAR_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/cache/mode/route.ts b/src/app/api/komga/cache/mode/route.ts index e92375b..74de763 100644 --- a/src/app/api/komga/cache/mode/route.ts +++ b/src/app/api/komga/cache/mode/route.ts @@ -5,7 +5,7 @@ import { ServerCacheService, } from "@/lib/services/server-cache.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; export async function GET() { try { @@ -17,7 +17,7 @@ export async function GET() { { error: { code: ERROR_CODES.CACHE.MODE_FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CACHE.MODE_FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.CACHE.MODE_FETCH_ERROR), }, }, { status: 500 } @@ -33,7 +33,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.CACHE.INVALID_MODE, - message: ERROR_MESSAGES[ERROR_CODES.CACHE.INVALID_MODE], + message: getErrorMessage(ERROR_CODES.CACHE.INVALID_MODE), }, }, { status: 400 } @@ -49,7 +49,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.CACHE.MODE_UPDATE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CACHE.MODE_UPDATE_ERROR], + message: getErrorMessage(ERROR_CODES.CACHE.MODE_UPDATE_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/config/route.ts b/src/app/api/komga/config/route.ts index 28f54aa..16c5723 100644 --- a/src/app/api/komga/config/route.ts +++ b/src/app/api/komga/config/route.ts @@ -1,8 +1,8 @@ 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"; import { KomgaConfig, KomgaConfigData } from "@/types/komga"; +import { getErrorMessage } from "@/utils/errors"; export const dynamic = "force-dynamic"; @@ -22,7 +22,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED, - message: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED], + message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED), }, }, { status: 401 } @@ -32,7 +32,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.CONFIG.SAVE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CONFIG.SAVE_ERROR], + message: getErrorMessage(ERROR_CODES.CONFIG.SAVE_ERROR), }, }, { status: 500 } @@ -53,7 +53,7 @@ export async function GET() { { error: { code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED, - message: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED], + message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED), }, }, { status: 401 } @@ -64,7 +64,7 @@ export async function GET() { { error: { code: ERROR_CODES.KOMGA.MISSING_CONFIG, - message: ERROR_MESSAGES[ERROR_CODES.KOMGA.MISSING_CONFIG], + message: getErrorMessage(ERROR_CODES.KOMGA.MISSING_CONFIG), }, }, { status: 404 } @@ -75,7 +75,7 @@ export async function GET() { { error: { code: ERROR_CODES.CONFIG.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CONFIG.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.CONFIG.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/favorites/route.ts b/src/app/api/komga/favorites/route.ts index a91e8cc..c8bf3b1 100644 --- a/src/app/api/komga/favorites/route.ts +++ b/src/app/api/komga/favorites/route.ts @@ -1,8 +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"; +import { getErrorMessage } from "@/utils/errors"; export async function GET() { try { @@ -15,7 +15,7 @@ export async function GET() { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -25,7 +25,7 @@ export async function GET() { { error: { code: ERROR_CODES.FAVORITE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.FAVORITE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.FAVORITE.FETCH_ERROR), }, }, { status: 500 } @@ -45,7 +45,7 @@ export async function POST(request: Request) { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -55,7 +55,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.FAVORITE.ADD_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.FAVORITE.ADD_ERROR], + message: getErrorMessage(ERROR_CODES.FAVORITE.ADD_ERROR), }, }, { status: 500 } @@ -75,7 +75,7 @@ export async function DELETE(request: Request) { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -85,7 +85,7 @@ export async function DELETE(request: Request) { { error: { code: ERROR_CODES.FAVORITE.DELETE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.FAVORITE.DELETE_ERROR], + message: getErrorMessage(ERROR_CODES.FAVORITE.DELETE_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/route.ts b/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/route.ts index a266567..7da585c 100644 --- a/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/route.ts +++ b/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/route.ts @@ -1,8 +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"; +import { getErrorMessage } from "@/utils/errors"; export const dynamic = "force-dynamic"; @@ -20,7 +20,7 @@ export async function GET( { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -30,7 +30,7 @@ export async function GET( { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/thumbnail/route.ts b/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/thumbnail/route.ts index 2634874..eb529d1 100644 --- a/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/thumbnail/route.ts +++ b/src/app/api/komga/images/books/[bookId]/pages/[pageNumber]/thumbnail/route.ts @@ -1,8 +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"; +import { getErrorMessage } from "@/utils/errors"; export const dynamic = "force-dynamic"; @@ -18,7 +18,7 @@ export async function GET( { error: { code: ERROR_CODES.BOOK.PAGES_FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.BOOK.PAGES_FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.BOOK.PAGES_FETCH_ERROR), }, }, { status: 400 } @@ -34,7 +34,7 @@ export async function GET( { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -44,7 +44,7 @@ export async function GET( { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/images/books/[bookId]/thumbnail/route.ts b/src/app/api/komga/images/books/[bookId]/thumbnail/route.ts index d514f17..720fedb 100644 --- a/src/app/api/komga/images/books/[bookId]/thumbnail/route.ts +++ b/src/app/api/komga/images/books/[bookId]/thumbnail/route.ts @@ -1,8 +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"; +import { getErrorMessage } from "@/utils/errors"; export async function GET(request: NextRequest, { params }: { params: { bookId: string } }) { try { @@ -15,7 +15,7 @@ export async function GET(request: NextRequest, { params }: { params: { bookId: { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -25,7 +25,7 @@ export async function GET(request: NextRequest, { params }: { params: { bookId: { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/images/series/[seriesId]/first-page/route.ts b/src/app/api/komga/images/series/[seriesId]/first-page/route.ts index 82329dc..ba7243a 100644 --- a/src/app/api/komga/images/series/[seriesId]/first-page/route.ts +++ b/src/app/api/komga/images/series/[seriesId]/first-page/route.ts @@ -1,8 +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"; +import { getErrorMessage } from "@/utils/errors"; export const dynamic = "force-dynamic"; @@ -17,7 +17,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -27,7 +27,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/images/series/[seriesId]/thumbnail/route.ts b/src/app/api/komga/images/series/[seriesId]/thumbnail/route.ts index 584b926..6f748be 100644 --- a/src/app/api/komga/images/series/[seriesId]/thumbnail/route.ts +++ b/src/app/api/komga/images/series/[seriesId]/thumbnail/route.ts @@ -1,8 +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"; +import { getErrorMessage } from "@/utils/errors"; export async function GET(request: NextRequest, { params }: { params: { seriesId: string } }) { try { @@ -15,7 +15,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -25,7 +25,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId { error: { code: ERROR_CODES.IMAGE.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.IMAGE.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/libraries/route.ts b/src/app/api/komga/libraries/route.ts index 4fb93cf..a0d3172 100644 --- a/src/app/api/komga/libraries/route.ts +++ b/src/app/api/komga/libraries/route.ts @@ -1,9 +1,9 @@ import { NextResponse } from "next/server"; import { LibraryService } from "@/lib/services/library.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; import { AppError } from "@/utils/errors"; import { KomgaLibrary } from "@/types/komga"; +import { getErrorMessage } from "@/utils/errors"; export const dynamic = "force-dynamic"; export async function GET() { @@ -17,7 +17,7 @@ export async function GET() { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -27,7 +27,7 @@ export async function GET() { { error: { code: ERROR_CODES.LIBRARY.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.LIBRARY.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.LIBRARY.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/series/[seriesId]/route.ts b/src/app/api/komga/series/[seriesId]/route.ts index 99d3948..78fb74c 100644 --- a/src/app/api/komga/series/[seriesId]/route.ts +++ b/src/app/api/komga/series/[seriesId]/route.ts @@ -1,9 +1,9 @@ import { 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"; import { KomgaSeries } from "@/types/komga"; +import { getErrorMessage } from "@/utils/errors"; export const dynamic = "force-dynamic"; @@ -18,7 +18,7 @@ export async function GET(request: Request, { params }: { params: { seriesId: st { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -28,7 +28,7 @@ export async function GET(request: Request, { params }: { params: { seriesId: st { error: { code: ERROR_CODES.SERIES.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.SERIES.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.SERIES.FETCH_ERROR), }, }, { status: 500 } diff --git a/src/app/api/komga/test/route.ts b/src/app/api/komga/test/route.ts index aaa3ecb..c428c01 100644 --- a/src/app/api/komga/test/route.ts +++ b/src/app/api/komga/test/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { TestService } from "@/lib/services/test.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; import { KomgaLibrary } from "@/types/komga"; export async function POST(request: Request) { @@ -25,7 +25,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.KOMGA.CONNECTION_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.KOMGA.CONNECTION_ERROR], + message: getErrorMessage(ERROR_CODES.KOMGA.CONNECTION_ERROR), }, }, { status: 400 } diff --git a/src/app/api/komga/ttl-config/route.ts b/src/app/api/komga/ttl-config/route.ts index f41c87f..47f9ee4 100644 --- a/src/app/api/komga/ttl-config/route.ts +++ b/src/app/api/komga/ttl-config/route.ts @@ -1,8 +1,8 @@ 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"; import { TTLConfig } from "@/types/komga"; +import { getErrorMessage } from "@/utils/errors"; export async function GET() { try { @@ -16,7 +16,7 @@ export async function GET() { { error: { code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED, - message: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED], + message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED), }, }, { status: 401 } @@ -27,7 +27,7 @@ export async function GET() { { error: { code: ERROR_CODES.CONFIG.TTL_FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CONFIG.TTL_FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.CONFIG.TTL_FETCH_ERROR), }, }, { status: 500 } @@ -58,7 +58,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED, - message: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED], + message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED), }, }, { status: 401 } @@ -68,7 +68,7 @@ export async function POST(request: Request) { { error: { code: ERROR_CODES.CONFIG.TTL_SAVE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.CONFIG.TTL_SAVE_ERROR], + message: getErrorMessage(ERROR_CODES.CONFIG.TTL_SAVE_ERROR), }, }, { status: 500 } diff --git a/src/app/api/preferences/route.ts b/src/app/api/preferences/route.ts index 0ca05fd..7d06516 100644 --- a/src/app/api/preferences/route.ts +++ b/src/app/api/preferences/route.ts @@ -1,9 +1,9 @@ import { NextRequest, NextResponse } from "next/server"; import { PreferencesService } from "@/lib/services/preferences.service"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; import { AppError } from "@/utils/errors"; import { UserPreferences } from "@/types/preferences"; +import { getErrorMessage } from "@/utils/errors"; export async function GET() { try { @@ -16,7 +16,7 @@ export async function GET() { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -26,7 +26,7 @@ export async function GET() { { error: { code: ERROR_CODES.PREFERENCES.FETCH_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.PREFERENCES.FETCH_ERROR], + message: getErrorMessage(ERROR_CODES.PREFERENCES.FETCH_ERROR), }, }, { status: 500 } @@ -48,7 +48,7 @@ export async function PUT(request: NextRequest) { { error: { code: error.code, - message: ERROR_MESSAGES[error.code], + message: getErrorMessage(error.code), }, }, { status: 500 } @@ -58,7 +58,7 @@ export async function PUT(request: NextRequest) { { error: { code: ERROR_CODES.PREFERENCES.UPDATE_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.PREFERENCES.UPDATE_ERROR], + message: getErrorMessage(ERROR_CODES.PREFERENCES.UPDATE_ERROR), }, }, { status: 500 } diff --git a/src/components/auth/RegisterForm.tsx b/src/components/auth/RegisterForm.tsx index 6947eb4..7df3f73 100644 --- a/src/components/auth/RegisterForm.tsx +++ b/src/components/auth/RegisterForm.tsx @@ -5,7 +5,7 @@ import { useRouter } from "next/navigation"; import { authService } from "@/lib/services/auth.service"; import { AuthError } from "@/types/auth"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; import { ErrorMessage } from "@/components/ui/ErrorMessage"; import { useTranslate } from "@/hooks/useTranslate"; @@ -32,7 +32,7 @@ export function RegisterForm({ from }: RegisterFormProps) { if (password !== confirmPassword) { setError({ code: ERROR_CODES.AUTH.PASSWORD_MISMATCH, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.PASSWORD_MISMATCH], + message: getErrorMessage(ERROR_CODES.AUTH.PASSWORD_MISMATCH), }); setIsLoading(false); return; diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index d3e219f..0f8785c 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -9,7 +9,7 @@ import { KomgaLibrary, KomgaSeries } from "@/types/komga"; import { usePreferences } from "@/contexts/PreferencesContext"; import { AppError } from "@/utils/errors"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; import { useToast } from "@/components/ui/use-toast"; import { useTranslate } from "@/hooks/useTranslate"; @@ -47,7 +47,7 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) { description: error instanceof AppError ? error.message - : ERROR_MESSAGES[ERROR_CODES.LIBRARY.FETCH_ERROR], + : getErrorMessage(ERROR_CODES.LIBRARY.FETCH_ERROR), variant: "destructive", }); setLibraries([]); @@ -88,7 +88,7 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) { description: error instanceof AppError ? error.message - : ERROR_MESSAGES[ERROR_CODES.FAVORITE.FETCH_ERROR], + : getErrorMessage(ERROR_CODES.FAVORITE.FETCH_ERROR), variant: "destructive", }); setFavorites([]); @@ -139,7 +139,9 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) { toast({ title: "Erreur", description: - error instanceof AppError ? error.message : ERROR_MESSAGES[ERROR_CODES.AUTH.LOGOUT_ERROR], + error instanceof AppError + ? error.message + : getErrorMessage(ERROR_CODES.AUTH.LOGOUT_ERROR), variant: "destructive", }); } diff --git a/src/components/series/SeriesHeader.tsx b/src/components/series/SeriesHeader.tsx index 85d0e74..5b783e4 100644 --- a/src/components/series/SeriesHeader.tsx +++ b/src/components/series/SeriesHeader.tsx @@ -9,7 +9,7 @@ import { Cover } from "@/components/ui/cover"; import { RefreshButton } from "@/components/library/RefreshButton"; import { AppError } from "@/utils/errors"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; import { useTranslate } from "@/hooks/useTranslate"; interface SeriesHeaderProps { @@ -38,7 +38,7 @@ export const SeriesHeader = ({ series, refreshSeries }: SeriesHeaderProps) => { description: error instanceof AppError ? error.message - : ERROR_MESSAGES[ERROR_CODES.FAVORITE.NETWORK_ERROR], + : getErrorMessage(ERROR_CODES.FAVORITE.NETWORK_ERROR), variant: "destructive", }); } @@ -80,7 +80,7 @@ export const SeriesHeader = ({ series, refreshSeries }: SeriesHeaderProps) => { description: error instanceof AppError ? error.message - : ERROR_MESSAGES[ERROR_CODES.FAVORITE.NETWORK_ERROR], + : getErrorMessage(ERROR_CODES.FAVORITE.NETWORK_ERROR), variant: "destructive", }); } diff --git a/src/lib/services/auth.service.ts b/src/lib/services/auth.service.ts index 549b617..f311f47 100644 --- a/src/lib/services/auth.service.ts +++ b/src/lib/services/auth.service.ts @@ -2,7 +2,7 @@ import { AuthError } from "@/types/auth"; import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; +import { getErrorMessage } from "@/utils/errors"; class AuthService { private static instance: AuthService; @@ -42,7 +42,7 @@ class AuthService { } throw { code: ERROR_CODES.AUTH.INVALID_CREDENTIALS, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.INVALID_CREDENTIALS], + message: getErrorMessage(ERROR_CODES.AUTH.INVALID_CREDENTIALS), } as AuthError; } } @@ -70,7 +70,7 @@ class AuthService { } throw { code: ERROR_CODES.AUTH.INVALID_USER_DATA, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.INVALID_USER_DATA], + message: getErrorMessage(ERROR_CODES.AUTH.INVALID_USER_DATA), } as AuthError; } } @@ -94,7 +94,7 @@ class AuthService { } throw { code: ERROR_CODES.AUTH.LOGOUT_ERROR, - message: ERROR_MESSAGES[ERROR_CODES.AUTH.LOGOUT_ERROR], + message: getErrorMessage(ERROR_CODES.AUTH.LOGOUT_ERROR), } as AuthError; } } diff --git a/src/middleware.ts b/src/middleware.ts index c5904ff..655f2af 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,8 +1,9 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { ERROR_CODES } from "./constants/errorCodes"; -import { ERROR_MESSAGES } from "./constants/errorMessages"; import { UserData } from "./lib/services/auth-server.service"; +import { getErrorMessage } from "./utils/errors"; +import i18nServer from "./i18n/i18n-server"; // Initialisation de i18n côté serveur // Routes qui ne nécessitent pas d'authentification const publicRoutes = ["/login", "/register", "/images"]; @@ -16,10 +17,10 @@ const defaultLocale = "fr"; export function middleware(request: NextRequest) { const { pathname } = request.nextUrl; - + console.log("ICIIII"); // Gestion de la langue let locale = request.cookies.get("NEXT_LOCALE")?.value; - + i18nServer.changeLanguage(locale); // Si pas de cookie de langue ou langue non supportée, on utilise la langue par défaut if (!locale || !locales.includes(locale)) { locale = defaultLocale; @@ -30,7 +31,7 @@ export function middleware(request: NextRequest) { path: "/", maxAge: 365 * 24 * 60 * 60, // 1 an }); - + i18nServer.changeLanguage(locale); return response; } @@ -57,7 +58,7 @@ export function middleware(request: NextRequest) { if (!user || !user.value) { if (pathname.startsWith("/api/")) { return NextResponse.json( - { error: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.UNAUTHORIZED] }, + { error: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED) }, { status: 401 } ); } @@ -69,13 +70,13 @@ export function middleware(request: NextRequest) { try { const userData: UserData = JSON.parse(atob(user.value)); if (!userData || !userData.authenticated || !userData.id || !userData.email) { - throw new Error(ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.INVALID_SESSION]); + throw new Error(getErrorMessage(ERROR_CODES.MIDDLEWARE.INVALID_SESSION)); } } catch (error) { console.error("Erreur de validation du cookie:", error); if (pathname.startsWith("/api/")) { return NextResponse.json( - { error: ERROR_MESSAGES[ERROR_CODES.MIDDLEWARE.INVALID_TOKEN] }, + { error: getErrorMessage(ERROR_CODES.MIDDLEWARE.INVALID_TOKEN) }, { status: 401 } ); }