#199 : Test de correction pour postgre.

This commit is contained in:
Loic Masi
2026-04-08 20:36:30 +02:00
parent 5202e66df1
commit 6610094d16
2 changed files with 36 additions and 5 deletions

View File

@@ -3,6 +3,8 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Npgsql;
using Webzine.EntitiesContext; using Webzine.EntitiesContext;
using Webzine.Entity; using Webzine.Entity;
using Webzine.Repository.Contracts; using Webzine.Repository.Contracts;
@@ -38,7 +40,19 @@ public class DbCommentaireRepository : ICommentaireRepository
} }
catch (DbUpdateException dbex) catch (DbUpdateException dbex)
{ {
this.logger.LogError(dbex, "Erreur de base de données lors de l'ajout du commentaire de l'auteur : {Auteur}", commentaire?.Auteur); PostgresException? postgresException = dbex.InnerException as PostgresException;
this.logger.LogError(
dbex,
"Erreur de base de données lors de l'ajout du commentaire. Auteur: {Auteur} | IdTitre: {IdTitre} | DateCreation: {DateCreation:o} | PostgresCode: {PostgresCode} | Detail: {Detail} | Constraint: {Constraint} | Column: {Column} | Table: {Table}",
commentaire?.Auteur,
commentaire?.IdTitre,
commentaire?.DateCreation,
postgresException?.SqlState,
postgresException?.Detail,
postgresException?.ConstraintName,
postgresException?.ColumnName,
postgresException?.TableName);
throw; throw;
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
/// <summary> /// <summary>
/// Filtre d'exception global qui intercepte toute exception non gérée et la journalise automatiquement. /// Filtre d'exception global qui intercepte toute exception non geree et la journalise automatiquement.
/// </summary> /// </summary>
public class GlobalExceptionFilter : IExceptionFilter public class GlobalExceptionFilter : IExceptionFilter
{ {
@@ -13,7 +13,7 @@ public class GlobalExceptionFilter : IExceptionFilter
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="GlobalExceptionFilter"/> class. /// Initializes a new instance of the <see cref="GlobalExceptionFilter"/> class.
/// </summary> /// </summary>
/// <param name="logger">Service de journalisation injecté.</param> /// <param name="logger">Service de journalisation injecte.</param>
public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger) public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
{ {
this.logger = logger; this.logger = logger;
@@ -22,11 +22,14 @@ public class GlobalExceptionFilter : IExceptionFilter
/// <inheritdoc/> /// <inheritdoc/>
public void OnException(ExceptionContext context) public void OnException(ExceptionContext context)
{ {
string detail = BuildExceptionDetail(context.Exception);
this.logger.LogError( this.logger.LogError(
context.Exception, context.Exception,
"Erreur non gérée dans {Action} : {Message}", "Erreur non geree dans {Action} : {Message} | Details: {Details}",
context.ActionDescriptor.DisplayName, context.ActionDescriptor.DisplayName,
context.Exception.Message); context.Exception.Message,
detail);
context.Result = new ObjectResult(new context.Result = new ObjectResult(new
{ {
@@ -39,4 +42,18 @@ public class GlobalExceptionFilter : IExceptionFilter
context.ExceptionHandled = true; context.ExceptionHandled = true;
} }
private static string BuildExceptionDetail(Exception exception)
{
var messages = new List<string>();
Exception? current = exception;
while (current != null)
{
messages.Add($"{current.GetType().Name}: {current.Message}");
current = current.InnerException;
}
return string.Join(" --> ", messages);
}
} }