refacto: error and error codes in services

This commit is contained in:
Julien Froidefond
2025-02-25 06:39:19 +01:00
parent d4871d1afb
commit 4b710cbac2
16 changed files with 389 additions and 125 deletions

View File

@@ -2,6 +2,8 @@ import { AuthConfig } from "@/types/auth";
import { getServerCacheService } from "./server-cache.service";
import { ConfigDBService } from "./config-db.service";
import { DebugService } from "./debug.service";
import { ERROR_CODES } from "../../constants/errorCodes";
import { AppError } from "../../utils/errors";
// Types de cache disponibles
export type CacheType = "DEFAULT" | "HOME" | "LIBRARIES" | "SERIES" | "BOOKS" | "IMAGES";
@@ -25,13 +27,13 @@ export abstract class BaseApiService {
};
} catch (error) {
console.error("Erreur lors de la récupération de la configuration:", error);
throw new Error("Configuration Komga non trouvée");
throw new AppError(ERROR_CODES.KOMGA.MISSING_CONFIG, {}, error);
}
}
protected static getAuthHeaders(config: AuthConfig): Headers {
if (!config.authHeader) {
throw new Error("Credentials Komga manquants");
throw new AppError(ERROR_CODES.KOMGA.MISSING_CREDENTIALS);
}
return new Headers({
@@ -56,16 +58,6 @@ export abstract class BaseApiService {
}
}
protected static handleError(error: unknown, defaultMessage: string): never {
console.error("API Error:", error);
if (error instanceof Error) {
throw error;
}
throw new Error(defaultMessage);
}
protected static buildUrl(
config: AuthConfig,
path: string,
@@ -114,7 +106,10 @@ export abstract class BaseApiService {
});
if (!response.ok) {
throw new Error(`Erreur HTTP: ${response.status} ${response.statusText}`);
throw new AppError(ERROR_CODES.KOMGA.HTTP_ERROR, {
status: response.status,
statusText: response.statusText,
});
}
return options.isImage ? response : response.json();