namespace Webzine.WebApplication.Filters; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; /// /// Filtre d'exception global qui intercepte toute exception non geree et la journalise automatiquement. /// public class GlobalExceptionFilter : IExceptionFilter { private readonly ILogger logger; /// /// Initializes a new instance of the class. /// /// Service de journalisation injecte. public GlobalExceptionFilter(ILogger logger) { this.logger = logger; } /// public void OnException(ExceptionContext context) { string detail = BuildExceptionDetail(context.Exception); this.logger.LogError( context.Exception, "Erreur non geree dans {Action} : {Message} | Details: {Details}", context.ActionDescriptor.DisplayName, context.Exception.Message, detail); context.Result = new ObjectResult(new { erreur = "Une erreur inattendue est survenue.", detail = context.Exception.Message, }) { StatusCode = StatusCodes.Status500InternalServerError, }; context.ExceptionHandled = true; } private static string BuildExceptionDetail(Exception exception) { var messages = new List(); Exception? current = exception; while (current != null) { messages.Add($"{current.GetType().Name}: {current.Message}"); current = current.InnerException; } return string.Join(" --> ", messages); } }