From 08d93474f4c3c9868f278b6874aa9d854b15e641 Mon Sep 17 00:00:00 2001 From: "josephine.vetu" Date: Thu, 26 Mar 2026 13:55:57 +0100 Subject: [PATCH] Ajout de try catch pour une meilleure gestion d'erreurs. --- Webzine.Repository/DbArtisteRepository.cs | 60 +++++++++++++------- Webzine.Repository/LocalArtisteRepository.cs | 37 ++++++------ 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Webzine.Repository/DbArtisteRepository.cs b/Webzine.Repository/DbArtisteRepository.cs index 5aa9bb7..4fa8e18 100644 --- a/Webzine.Repository/DbArtisteRepository.cs +++ b/Webzine.Repository/DbArtisteRepository.cs @@ -1,5 +1,6 @@ namespace Webzine.Repository { + using System.Data.Common; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Webzine.EntitiesContext; @@ -15,7 +16,6 @@ private WebzineDbContext _context; private readonly ILogger _logger; - /// /// Initializes a new instance of the class. /// @@ -32,14 +32,14 @@ { try { - if (artiste == null) - { - throw new ArgumentNullException(nameof(artiste), "L'artiste à ajouter ne peut pas être null."); - } - this._context.Artistes.Add(artiste); this._context.SaveChanges(); } + catch (DbUpdateException dbex) + { + this._logger.LogError(dbex, "Erreur de base de données lors de l'ajout de l'artiste: {id}", artiste.IdArtiste); + throw; + } catch (Exception ex) { this._logger.LogError(ex, "Une erreur est survenue lors de l'ajout de l'artiste {Nom}.", artiste?.Nom); @@ -50,7 +50,7 @@ /// public void Delete(Artiste artiste) { - try + try { if (artiste == null) { @@ -60,6 +60,11 @@ this._context.Artistes.Remove(artiste); this._context.SaveChanges(); } + catch (DbUpdateException dbex) + { + this._logger.LogError(dbex, "Erreur de base de données lors de la suppression de l'artiste: {Id}", artiste.IdArtiste); + throw; + } catch (Exception ex) { this._logger.LogError(ex, "Une erreur est survenue lors de la suppression de l'artiste {Nom}.", artiste?.Nom); @@ -70,35 +75,41 @@ /// public Artiste Find(int id) { - Artiste artiste = this._context.Artistes - .Include(a => a.Titres) - .FirstOrDefault(a => a.IdArtiste == id); - if (artiste == null) + try { - this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id); + Artiste artiste = this._context.Artistes + .Include(a => a.Titres) + .First(a => a.IdArtiste == id); + return artiste; + } + catch (Exception ex) + { + this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste: {Id}", id); + throw; } - return artiste; } /// public Artiste FindByName(string nom) { - if (string.IsNullOrWhiteSpace(nom)) + try { - this._logger.LogWarning("Tentative de recherche avec un nom vide ou null."); - return null; - } - var artiste = this._context.Artistes .Include(a => a.Titres) .FirstOrDefault(a => a.Nom == nom); if (artiste == null) - { - this._logger.LogWarning("Recherche Nom : Aucun artiste trouvé pour '{Nom}'.", nom); - } - + { + this._logger.LogWarning("Pas d'artiste au nom {Nom}", nom); + artiste = new Artiste(); + } return artiste; + } + catch (Exception ex) + { + this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom); + throw; + } } /// @@ -132,6 +143,11 @@ this._context.SaveChanges(); this._logger.LogInformation("Artiste {Id} ({Nom}) mis à jour avec succès.", artiste.IdArtiste, artiste.Nom); } + catch (DbUpdateException ex) + { + this._logger.LogError(ex, "Erreur de base de données lors de la mise à jour de l'artiste ID: {IdArtiste}", artiste.IdArtiste); + throw; + } catch (Exception ex) { this._logger.LogError(ex, "Erreur lors de la mise à jour de l'artiste {Id}.", artiste.IdArtiste); diff --git a/Webzine.Repository/LocalArtisteRepository.cs b/Webzine.Repository/LocalArtisteRepository.cs index 0841d3c..36b30c9 100644 --- a/Webzine.Repository/LocalArtisteRepository.cs +++ b/Webzine.Repository/LocalArtisteRepository.cs @@ -30,12 +30,6 @@ { try { - if (artiste == null) - { - this._logger.LogError("L'artiste à ajouter ne peut pas être null."); - throw new ArgumentNullException(nameof(artiste)); - } - if (this._artistes.Any(a => a.IdArtiste == artiste.IdArtiste)) { this._logger.LogWarning("Un artiste avec l'ID {Id} existe déjà. L'ajout est ignoré.", artiste.IdArtiste); @@ -70,23 +64,32 @@ /// public Artiste Find(int id) { - Artiste artiste = this._artistes.FirstOrDefault(a => a.IdArtiste == id); - if (artiste == null) + try { - this._logger.LogWarning("Aucun artiste trouvé avec l'identifiant {Id}", id); + Artiste artiste = this._artistes.First(a => a.IdArtiste == id); + return artiste; + } + catch (Exception ex) + { + this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec ID: {Id}", id); + throw; } - return artiste; } /// public Artiste FindByName(string nom) { - Artiste artiste = this._artistes.FirstOrDefault(a => a.Nom == nom); - if (artiste == null) + try { - this._logger.LogWarning("Aucun artiste trouvé avec le nom {Nom}", nom); + Artiste artiste = this._artistes.First(a => a.Nom == nom); + return artiste; } - return artiste; + catch (Exception ex) + { + this._logger.LogError(ex, "Erreur lors de la recherche de l'artiste avec le nom: {Nom}", nom); + throw; + } + } /// @@ -99,12 +102,6 @@ /// public void Update(Artiste artiste) { - if (artiste == null) - { - this._logger.LogError("L'artiste à mettre à jour ne peut pas être null."); - throw new ArgumentNullException(nameof(artiste)); - } - try { var artisteToUpdate = this._artistes.FirstOrDefault(a => a.IdArtiste == artiste.IdArtiste);