refacto: error and types

This commit is contained in:
Julien Froidefond
2025-02-27 21:59:14 +01:00
parent ea51ff53a9
commit 279f6c6e88
37 changed files with 800 additions and 684 deletions

View File

@@ -3,6 +3,7 @@ import { AuthServerService } from "@/lib/services/auth-server.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
import { UserData } from "@/lib/services/auth-server.service";
import { getErrorMessage } from "@/utils/errors";
export async function POST(request: Request) {
try {
@@ -20,9 +21,7 @@ export async function POST(request: Request) {
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
code: error.code,
},
error: AppError,
},
{ status: 401 }
);
@@ -35,7 +34,9 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.AUTH.INVALID_CREDENTIALS,
},
name: "Invalid credentials",
message: getErrorMessage(ERROR_CODES.AUTH.INVALID_CREDENTIALS),
} as AppError,
},
{ status: 500 }
);

View File

@@ -1,6 +1,8 @@
import { NextResponse } from "next/server";
import { cookies } from "next/headers";
import { ERROR_CODES } from "@/constants/errorCodes";
import { getErrorMessage } from "@/utils/errors";
import { AppErrorType } from "@/types/global";
export async function POST() {
try {
@@ -13,7 +15,9 @@ export async function POST() {
{
error: {
code: ERROR_CODES.AUTH.LOGOUT_ERROR,
},
name: "Logout error",
message: getErrorMessage(ERROR_CODES.AUTH.LOGOUT_ERROR),
} as AppErrorType,
},
{ status: 500 }
);

View File

@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { AuthServerService, UserData } from "@/lib/services/auth-server.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
import { getErrorMessage } from "@/utils/errors";
export async function POST(request: Request) {
try {
@@ -24,9 +25,7 @@ export async function POST(request: Request) {
: 500;
return NextResponse.json(
{
error: {
code: error.code,
},
error: AppError,
},
{ status }
);
@@ -39,6 +38,8 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.AUTH.INVALID_USER_DATA,
name: "Invalid user data",
message: getErrorMessage(ERROR_CODES.AUTH.INVALID_USER_DATA),
},
},
{ status: 500 }

View File

@@ -15,8 +15,9 @@ export async function GET() {
{
error: {
code: error.code,
name: "Debug fetch error",
message: getErrorMessage(error.code),
},
} as AppError,
},
{ status: 500 }
);
@@ -25,8 +26,9 @@ export async function GET() {
{
error: {
code: ERROR_CODES.DEBUG.FETCH_ERROR,
name: "Debug fetch error",
message: getErrorMessage(ERROR_CODES.DEBUG.FETCH_ERROR),
},
} as AppError,
},
{ status: 500 }
);
@@ -47,8 +49,9 @@ export async function POST(request: NextRequest) {
{
error: {
code: error.code,
name: "Debug save error",
message: getErrorMessage(error.code),
},
} as AppError,
},
{ status: 500 }
);
@@ -57,8 +60,9 @@ export async function POST(request: NextRequest) {
{
error: {
code: ERROR_CODES.DEBUG.SAVE_ERROR,
name: "Debug save error",
message: getErrorMessage(ERROR_CODES.DEBUG.SAVE_ERROR),
},
} as AppError,
},
{ status: 500 }
);
@@ -78,8 +82,9 @@ export async function DELETE() {
{
error: {
code: error.code,
name: "Debug clear error",
message: getErrorMessage(error.code),
},
} as AppError,
},
{ status: 500 }
);
@@ -88,8 +93,9 @@ export async function DELETE() {
{
error: {
code: ERROR_CODES.DEBUG.CLEAR_ERROR,
name: "Debug clear error",
message: getErrorMessage(ERROR_CODES.DEBUG.CLEAR_ERROR),
},
} as AppError,
},
{ status: 500 }
);

View File

@@ -17,6 +17,7 @@ export async function GET(
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},
@@ -41,6 +42,7 @@ export async function GET(
{
error: {
code: error.code,
name: "Image fetch error",
message: getErrorMessage(error.code),
},
},
@@ -51,6 +53,7 @@ export async function GET(
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},

View File

@@ -13,6 +13,7 @@ export async function PATCH(request: NextRequest, { params }: { params: { bookId
{
error: {
code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR,
name: "Progress update error",
message: getErrorMessage(ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR),
},
},
@@ -29,6 +30,7 @@ export async function PATCH(request: NextRequest, { params }: { params: { bookId
{
error: {
code: error.code,
name: "Progress update error",
message: getErrorMessage(error.code),
},
},
@@ -39,6 +41,7 @@ export async function PATCH(request: NextRequest, { params }: { params: { bookId
{
error: {
code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR,
name: "Progress update error",
message: getErrorMessage(ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR),
},
},
@@ -58,6 +61,7 @@ export async function DELETE(request: NextRequest, { params }: { params: { bookI
{
error: {
code: error.code,
name: "Progress delete error",
message: getErrorMessage(error.code),
},
},
@@ -68,6 +72,7 @@ export async function DELETE(request: NextRequest, { params }: { params: { bookI
{
error: {
code: ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR,
name: "Progress delete error",
message: getErrorMessage(ERROR_CODES.BOOK.PROGRESS_DELETE_ERROR),
},
},

View File

@@ -15,8 +15,9 @@ export async function GET(request: Request, { params }: { params: { bookId: stri
{
error: {
code: error.code,
name: "Book fetch error",
message: getErrorMessage(error.code),
},
} as AppError,
},
{ status: 500 }
);
@@ -25,8 +26,9 @@ export async function GET(request: Request, { params }: { params: { bookId: stri
{
error: {
code: ERROR_CODES.BOOK.NOT_FOUND,
name: "Book fetch error",
message: getErrorMessage(ERROR_CODES.BOOK.NOT_FOUND),
},
} as AppError,
},
{ status: 500 }
);

View File

@@ -14,6 +14,7 @@ export async function POST() {
{
error: {
code: ERROR_CODES.CACHE.CLEAR_ERROR,
name: "Cache clear error",
message: getErrorMessage(ERROR_CODES.CACHE.CLEAR_ERROR),
},
},

View File

@@ -17,6 +17,7 @@ export async function GET() {
{
error: {
code: ERROR_CODES.CACHE.MODE_FETCH_ERROR,
name: "Cache mode fetch error",
message: getErrorMessage(ERROR_CODES.CACHE.MODE_FETCH_ERROR),
},
},
@@ -33,6 +34,7 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.CACHE.INVALID_MODE,
name: "Invalid cache mode",
message: getErrorMessage(ERROR_CODES.CACHE.INVALID_MODE),
},
},
@@ -49,6 +51,7 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.CACHE.MODE_UPDATE_ERROR,
name: "Cache mode update error",
message: getErrorMessage(ERROR_CODES.CACHE.MODE_UPDATE_ERROR),
},
},

View File

@@ -22,6 +22,7 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
name: "Unauthorized",
message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED),
},
},
@@ -32,6 +33,7 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.CONFIG.SAVE_ERROR,
name: "Config save error",
message: getErrorMessage(ERROR_CODES.CONFIG.SAVE_ERROR),
},
},
@@ -53,6 +55,7 @@ export async function GET() {
{
error: {
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
name: "Unauthorized",
message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED),
},
},
@@ -64,6 +67,7 @@ export async function GET() {
{
error: {
code: ERROR_CODES.KOMGA.MISSING_CONFIG,
name: "Missing config",
message: getErrorMessage(ERROR_CODES.KOMGA.MISSING_CONFIG),
},
},
@@ -75,6 +79,7 @@ export async function GET() {
{
error: {
code: ERROR_CODES.CONFIG.FETCH_ERROR,
name: "Config fetch error",
message: getErrorMessage(ERROR_CODES.CONFIG.FETCH_ERROR),
},
},

View File

@@ -15,6 +15,7 @@ export async function GET() {
{
error: {
code: error.code,
name: "Favorite fetch error",
message: getErrorMessage(error.code),
},
},
@@ -25,6 +26,7 @@ export async function GET() {
{
error: {
code: ERROR_CODES.FAVORITE.FETCH_ERROR,
name: "Favorite fetch error",
message: getErrorMessage(ERROR_CODES.FAVORITE.FETCH_ERROR),
},
},
@@ -45,6 +47,7 @@ export async function POST(request: Request) {
{
error: {
code: error.code,
name: "Favorite add error",
message: getErrorMessage(error.code),
},
},
@@ -55,6 +58,7 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.FAVORITE.ADD_ERROR,
name: "Favorite add error",
message: getErrorMessage(ERROR_CODES.FAVORITE.ADD_ERROR),
},
},
@@ -75,6 +79,7 @@ export async function DELETE(request: Request) {
{
error: {
code: error.code,
name: "Favorite delete error",
message: getErrorMessage(error.code),
},
},
@@ -85,6 +90,7 @@ export async function DELETE(request: Request) {
{
error: {
code: ERROR_CODES.FAVORITE.DELETE_ERROR,
name: "Favorite delete error",
message: getErrorMessage(ERROR_CODES.FAVORITE.DELETE_ERROR),
},
},

View File

@@ -20,6 +20,7 @@ export async function GET(
{
error: {
code: error.code,
name: "Image fetch error",
message: getErrorMessage(error.code),
},
},
@@ -30,6 +31,7 @@ export async function GET(
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},

View File

@@ -18,6 +18,7 @@ export async function GET(
{
error: {
code: ERROR_CODES.BOOK.PAGES_FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.BOOK.PAGES_FETCH_ERROR),
},
},
@@ -34,6 +35,7 @@ export async function GET(
{
error: {
code: error.code,
name: "Image fetch error",
message: getErrorMessage(error.code),
},
},
@@ -44,6 +46,7 @@ export async function GET(
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},

View File

@@ -15,6 +15,7 @@ export async function GET(request: NextRequest, { params }: { params: { bookId:
{
error: {
code: error.code,
name: "Image fetch error",
message: getErrorMessage(error.code),
},
},
@@ -25,6 +26,7 @@ export async function GET(request: NextRequest, { params }: { params: { bookId:
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},

View File

@@ -17,6 +17,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId
{
error: {
code: error.code,
name: "Image fetch error",
message: getErrorMessage(error.code),
},
},
@@ -27,6 +28,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},

View File

@@ -15,6 +15,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId
{
error: {
code: error.code,
name: "Image fetch error",
message: getErrorMessage(error.code),
},
},
@@ -25,6 +26,7 @@ export async function GET(request: NextRequest, { params }: { params: { seriesId
{
error: {
code: ERROR_CODES.IMAGE.FETCH_ERROR,
name: "Image fetch error",
message: getErrorMessage(ERROR_CODES.IMAGE.FETCH_ERROR),
},
},

View File

@@ -17,6 +17,7 @@ export async function GET() {
{
error: {
code: error.code,
name: "Library fetch error",
message: getErrorMessage(error.code),
},
},
@@ -27,6 +28,7 @@ export async function GET() {
{
error: {
code: ERROR_CODES.LIBRARY.FETCH_ERROR,
name: "Library fetch error",
message: getErrorMessage(ERROR_CODES.LIBRARY.FETCH_ERROR),
},
},

View File

@@ -18,6 +18,7 @@ export async function GET(request: Request, { params }: { params: { seriesId: st
{
error: {
code: error.code,
name: "Series fetch error",
message: getErrorMessage(error.code),
},
},
@@ -28,6 +29,7 @@ export async function GET(request: Request, { params }: { params: { seriesId: st
{
error: {
code: ERROR_CODES.SERIES.FETCH_ERROR,
name: "Series fetch error",
message: getErrorMessage(ERROR_CODES.SERIES.FETCH_ERROR),
},
},

View File

@@ -25,6 +25,7 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.KOMGA.CONNECTION_ERROR,
name: "Connection error",
message: getErrorMessage(ERROR_CODES.KOMGA.CONNECTION_ERROR),
},
},

View File

@@ -11,10 +11,11 @@ export async function GET() {
} catch (error) {
console.error("Erreur lors de la récupération de la configuration TTL:", error);
if (error instanceof Error) {
if (error.message === "Utilisateur non authentifié") {
if (error.message === getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED)) {
return NextResponse.json(
{
error: {
name: "Unauthorized",
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED),
},
@@ -26,6 +27,7 @@ export async function GET() {
return NextResponse.json(
{
error: {
name: "TTL fetch error",
code: ERROR_CODES.CONFIG.TTL_FETCH_ERROR,
message: getErrorMessage(ERROR_CODES.CONFIG.TTL_FETCH_ERROR),
},
@@ -53,10 +55,14 @@ 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é") {
if (
error instanceof Error &&
error.message === getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED)
) {
return NextResponse.json(
{
error: {
name: "Unauthorized",
code: ERROR_CODES.MIDDLEWARE.UNAUTHORIZED,
message: getErrorMessage(ERROR_CODES.MIDDLEWARE.UNAUTHORIZED),
},
@@ -67,6 +73,7 @@ export async function POST(request: Request) {
return NextResponse.json(
{
error: {
name: "TTL save error",
code: ERROR_CODES.CONFIG.TTL_SAVE_ERROR,
message: getErrorMessage(ERROR_CODES.CONFIG.TTL_SAVE_ERROR),
},

View File

@@ -15,6 +15,7 @@ export async function GET() {
return NextResponse.json(
{
error: {
name: "Preferences fetch error",
code: error.code,
message: getErrorMessage(error.code),
},
@@ -25,6 +26,7 @@ export async function GET() {
return NextResponse.json(
{
error: {
name: "Preferences fetch error",
code: ERROR_CODES.PREFERENCES.FETCH_ERROR,
message: getErrorMessage(ERROR_CODES.PREFERENCES.FETCH_ERROR),
},
@@ -47,6 +49,7 @@ export async function PUT(request: NextRequest) {
return NextResponse.json(
{
error: {
name: "Preferences update error",
code: error.code,
message: getErrorMessage(error.code),
},
@@ -57,6 +60,7 @@ export async function PUT(request: NextRequest) {
return NextResponse.json(
{
error: {
name: "Preferences update error",
code: ERROR_CODES.PREFERENCES.UPDATE_ERROR,
message: getErrorMessage(ERROR_CODES.PREFERENCES.UPDATE_ERROR),
},

View File

@@ -8,7 +8,6 @@ import { ErrorMessage } from "@/components/ui/ErrorMessage";
import { LibraryResponse } from "@/types/library";
import { KomgaSeries, KomgaLibrary } from "@/types/komga";
import { UserPreferences } from "@/types/preferences";
import { ERROR_CODES } from "@/constants/errorCodes";
interface PageProps {
params: { libraryId: string };
@@ -99,13 +98,13 @@ async function LibraryPage({ params, searchParams }: PageProps) {
<h1 className="text-3xl font-bold">Séries</h1>
<RefreshButton libraryId={params.libraryId} refreshLibrary={refreshLibrary} />
</div>
<ErrorMessage errorCode="SERIES_FETCH_ERROR" />
<ErrorMessage error={error as Error} errorCode="SERIES_FETCH_ERROR" />
</div>
);
}
return (
<div className="container py-8 space-y-8">
<ErrorMessage errorCode="SERIES_FETCH_ERROR" />
<ErrorMessage error={error as Error} errorCode="SERIES_FETCH_ERROR" />
</div>
);
}

View File

@@ -5,7 +5,7 @@ import { revalidatePath } from "next/cache";
import { withPageTiming } from "@/lib/hoc/withPageTiming";
import { ErrorMessage } from "@/components/ui/ErrorMessage";
import { HomeData } from "@/lib/services/home.service";
import { ERROR_CODES } from "@/constants/errorCodes";
async function refreshHome() {
"use server";
@@ -32,7 +32,7 @@ async function HomePage() {
return (
<main className="container mx-auto px-4 py-8">
<ErrorMessage errorCode="HOME_FETCH_ERROR" />
<ErrorMessage error={error as Error} errorCode="HOME_FETCH_ERROR" />
</main>
);
}

View File

@@ -78,7 +78,7 @@ async function SeriesPage({ params, searchParams }: PageProps) {
return (
<div className="container py-8 space-y-8">
<h1 className="text-3xl font-bold">Série</h1>
<ErrorMessage errorCode="SERIES_FETCH_ERROR" />
<ErrorMessage error={error as Error} errorCode="SERIES_FETCH_ERROR" />
</div>
);
}