feat(i18n): first shoot on translated errors

This commit is contained in:
Julien Froidefond
2025-02-27 15:05:44 +01:00
parent 41228e59bc
commit ea51ff53a9
14 changed files with 54 additions and 38 deletions

View File

@@ -1,7 +1,6 @@
import { NextResponse } from "next/server";
import { AuthServerService } from "@/lib/services/auth-server.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { getErrorMessage } from "@/utils/errors";
import { AppError } from "@/utils/errors";
import { UserData } from "@/lib/services/auth-server.service";
@@ -23,7 +22,6 @@ export async function POST(request: Request) {
{
error: {
code: error.code,
message: getErrorMessage(error.code),
},
},
{ status: 401 }
@@ -37,7 +35,6 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.AUTH.INVALID_CREDENTIALS,
message: getErrorMessage(ERROR_CODES.AUTH.INVALID_CREDENTIALS),
},
},
{ status: 500 }

View File

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

View File

@@ -2,7 +2,6 @@ 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 {
@@ -27,7 +26,6 @@ export async function POST(request: Request) {
{
error: {
code: error.code,
message: getErrorMessage(error.code),
},
},
{ status }
@@ -41,7 +39,6 @@ export async function POST(request: Request) {
{
error: {
code: ERROR_CODES.AUTH.INVALID_USER_DATA,
message: getErrorMessage(ERROR_CODES.AUTH.INVALID_USER_DATA),
},
},
{ status: 500 }

View File

@@ -8,6 +8,8 @@ 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 };
searchParams: { page?: string; unread?: string; search?: string };
@@ -90,17 +92,20 @@ async function LibraryPage({ params, searchParams }: PageProps) {
</div>
);
} catch (error) {
if (error instanceof Error) {
return (
<div className="container py-8 space-y-8">
<div className="flex items-center justify-between">
<h1 className="text-3xl font-bold">Séries</h1>
<RefreshButton libraryId={params.libraryId} refreshLibrary={refreshLibrary} />
</div>
<ErrorMessage errorCode="SERIES_FETCH_ERROR" />
</div>
);
}
return (
<div className="container py-8 space-y-8">
<div className="flex items-center justify-between">
<h1 className="text-3xl font-bold">Séries</h1>
<RefreshButton libraryId={params.libraryId} refreshLibrary={refreshLibrary} />
</div>
<ErrorMessage
message={
error instanceof Error ? error.message : "Erreur lors de la récupération des séries"
}
/>
<ErrorMessage 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,9 +32,7 @@ async function HomePage() {
return (
<main className="container mx-auto px-4 py-8">
<ErrorMessage
message={error instanceof Error ? error.message : "Une erreur est survenue"}
/>
<ErrorMessage errorCode="HOME_FETCH_ERROR" />
</main>
);
}

View File

@@ -78,11 +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
message={
error instanceof Error ? error.message : "Erreur lors de la récupération de la série"
}
/>
<ErrorMessage errorCode="SERIES_FETCH_ERROR" />
</div>
);
}

View File

@@ -89,7 +89,7 @@ export function LoginForm({ from }: LoginFormProps) {
{t("login.form.remember")}
</label>
</div>
{error && <ErrorMessage message={error.message} variant="form" />}
{error && <ErrorMessage errorCode={error.code} variant="form" />}
<button
type="submit"
disabled={isLoading}

View File

@@ -5,7 +5,6 @@ import { useRouter } from "next/navigation";
import { authService } from "@/lib/services/auth.service";
import { AuthError } from "@/types/auth";
import { ERROR_CODES } from "@/constants/errorCodes";
import { getErrorMessage } from "@/utils/errors";
import { ErrorMessage } from "@/components/ui/ErrorMessage";
import { useTranslate } from "@/hooks/useTranslate";
@@ -32,7 +31,6 @@ export function RegisterForm({ from }: RegisterFormProps) {
if (password !== confirmPassword) {
setError({
code: ERROR_CODES.AUTH.PASSWORD_MISMATCH,
message: getErrorMessage(ERROR_CODES.AUTH.PASSWORD_MISMATCH),
});
setIsLoading(false);
return;
@@ -99,7 +97,7 @@ export function RegisterForm({ from }: RegisterFormProps) {
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
/>
</div>
{error && <ErrorMessage message={error.message} variant="form" />}
{error && <ErrorMessage errorCode={error.code} variant="form" />}
<button
type="submit"
disabled={isLoading}

View File

@@ -271,7 +271,7 @@ export function ClientSettings({ initialConfig, initialTTLConfig }: ClientSettin
{/* Messages de succès/erreur */}
{error && (
<div className="rounded-md bg-destructive/15 p-4">
<p className="text-sm text-destructive">{error.message}</p>
<p className="text-sm text-destructive">{t(`errors.${error.code}`)}</p>
</div>
)}
{success && (

View File

@@ -1,11 +1,17 @@
"use client";
import { AlertCircle } from "lucide-react";
import { useTranslate } from "@/hooks/useTranslate";
interface ErrorMessageProps {
message: string;
errorCode: string;
variant?: "default" | "form";
}
export const ErrorMessage = ({ message, variant = "default" }: ErrorMessageProps) => {
export const ErrorMessage = ({ errorCode, variant = "default" }: ErrorMessageProps) => {
const { t } = useTranslate();
const message = t(`errors.${errorCode}`);
if (variant === "form") {
return (
<div

View File

@@ -232,5 +232,18 @@
"pages": "{current}/{total} pages",
"totalPages": "{count} pages"
}
},
"errors": {
"AUTH_INVALID_CREDENTIALS": "Invalid credentials",
"AUTH_PASSWORD_MISMATCH": "Passwords do not match",
"AUTH_EMAIL_EXISTS": "Email already exists",
"AUTH_PASSWORD_NOT_STRONG": "Password is not strong enough",
"AUTH_LOGOUT_ERROR": "Logout error",
"AUTH_INVALID_USER_DATA": "Invalid user data",
"AUTH_UNAUTHENTICATED": "Unauthenticated",
"SERIES_FETCH_ERROR": "Error fetching series",
"HOME_FETCH_ERROR": "Error fetching home",
"SERIES_NO_BOOKS_FOUND": "No books found in the series"
}
}

View File

@@ -232,5 +232,18 @@
"pages": "{current}/{total} pages",
"totalPages": "{count} pages"
}
},
"errors": {
"AUTH_INVALID_CREDENTIALS": "Identifiants invalides",
"AUTH_PASSWORD_MISMATCH": "Les mots de passe ne correspondent pas",
"AUTH_EMAIL_EXISTS": "Cette adresse email existe déjà",
"AUTH_PASSWORD_NOT_STRONG": "Le mot de passe n'est pas assez fort",
"AUTH_LOGOUT_ERROR": "Erreur lors de la déconnexion",
"AUTH_INVALID_USER_DATA": "Données utilisateur invalides",
"AUTH_UNAUTHENTICATED": "Non authentifié",
"SERIES_FETCH_ERROR": "Erreur lors de la récupération des séries",
"HOME_FETCH_ERROR": "Erreur lors de la récupération de l'accueil",
"SERIES_NO_BOOKS_FOUND": "Aucun livre trouvé dans la série"
}
}

View File

@@ -2,7 +2,6 @@
import { AuthError } from "@/types/auth";
import { ERROR_CODES } from "@/constants/errorCodes";
import { getErrorMessage } from "@/utils/errors";
class AuthService {
private static instance: AuthService;
@@ -42,7 +41,6 @@ class AuthService {
}
throw {
code: ERROR_CODES.AUTH.INVALID_CREDENTIALS,
message: getErrorMessage(ERROR_CODES.AUTH.INVALID_CREDENTIALS),
} as AuthError;
}
}
@@ -70,7 +68,6 @@ class AuthService {
}
throw {
code: ERROR_CODES.AUTH.INVALID_USER_DATA,
message: getErrorMessage(ERROR_CODES.AUTH.INVALID_USER_DATA),
} as AuthError;
}
}
@@ -94,7 +91,6 @@ class AuthService {
}
throw {
code: ERROR_CODES.AUTH.LOGOUT_ERROR,
message: getErrorMessage(ERROR_CODES.AUTH.LOGOUT_ERROR),
} as AuthError;
}
}

View File

@@ -14,7 +14,6 @@ export interface AuthState {
export interface AuthError {
code: ErrorCode;
message: string;
}
// Deprecated - Use ErrorCode from @/constants/errorCodes instead