From f9eeb453ac3ce6e6b0cd8a8ecb8f021c940df268 Mon Sep 17 00:00:00 2001 From: Julien LEICHER Date: Tue, 14 Dec 2021 12:00:47 +0100 Subject: [PATCH] ajout CustomExceptionFilter --- .../Controllers/LinksController.cs | 2 ++ .../Filters/CustomExceptionFilter.cs | 15 ++++++++ Apps/HackerNet.Web/Program.cs | 35 ++++++++++++++++++- .../appsettings.Development.json | 2 +- 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs diff --git a/Apps/HackerNet.Web/Controllers/LinksController.cs b/Apps/HackerNet.Web/Controllers/LinksController.cs index 3796673..ace95bb 100644 --- a/Apps/HackerNet.Web/Controllers/LinksController.cs +++ b/Apps/HackerNet.Web/Controllers/LinksController.cs @@ -1,4 +1,5 @@ using HackerNet.Application; +using HackerNet.Web.Filters; using HackerNet.Web.Models; using Microsoft.AspNetCore.Mvc; @@ -22,6 +23,7 @@ public class LinksController : HackerController } [HttpGet] + [TypeFilter(typeof(CustomExceptionFilter))] public IActionResult Detail(Guid id) { var link = _linkService.GetLinkDetail(id); diff --git a/Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs b/Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs new file mode 100644 index 0000000..7eb5331 --- /dev/null +++ b/Apps/HackerNet.Web/Filters/CustomExceptionFilter.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace HackerNet.Web.Filters; + +public class CustomExceptionFilter : IExceptionFilter +{ + public void OnException(ExceptionContext context) + { + if (context.Exception is InvalidOperationException) + { + context.Result = new NotFoundResult(); + } + } +} \ No newline at end of file diff --git a/Apps/HackerNet.Web/Program.cs b/Apps/HackerNet.Web/Program.cs index 955b1e7..9d60a84 100644 --- a/Apps/HackerNet.Web/Program.cs +++ b/Apps/HackerNet.Web/Program.cs @@ -1,6 +1,7 @@ using HackerNet.Application; using HackerNet.Domain; using HackerNet.Infrastructure.Repositories.Memory; +using HackerNet.Web.Filters; var builder = WebApplication.CreateBuilder(args); @@ -15,7 +16,10 @@ builder.Services.AddSingleton(commentsRepository); builder.Services.AddSingleton(new MemoryReadStore(linksRepository, commentsRepository)); builder.Services.AddSingleton(); -builder.Services.AddControllersWithViews(); +builder.Services.AddControllersWithViews(o => +{ + o.Filters.Add(); +}); var app = builder.Build(); @@ -27,11 +31,40 @@ if (!app.Environment.IsDevelopment()) app.UseHsts(); } +app.Use(async (ctx, next) => +{ + var logger = ctx.RequestServices.GetRequiredService>(); + logger.LogDebug(">>>>>>>>>>>>>> Début requête"); + try + { + await next(); + } + catch (Exception e) + { + // Envoi app insights ou autre + logger.LogError(e.Message); + + throw e; + } + logger.LogDebug("<<<<<<<<<<<<<< Fin requête"); +}); + +app.Use(async (ctx, next) => +{ + var logger = ctx.RequestServices.GetRequiredService>(); + logger.LogDebug(">>>>>>>>>>>>>> Début requête 2"); + await next(); + logger.LogDebug("<<<<<<<<<<<<<< Fin requête 2"); +}); + app.UseHttpsRedirection(); app.UseStaticFiles(); +// app.UseStatusCodePages() + app.UseRouting(); +// app.UseAuthentication(); app.UseAuthorization(); app.MapControllerRoute( diff --git a/Apps/HackerNet.Web/appsettings.Development.json b/Apps/HackerNet.Web/appsettings.Development.json index ff66ba6..bc7bab3 100644 --- a/Apps/HackerNet.Web/appsettings.Development.json +++ b/Apps/HackerNet.Web/appsettings.Development.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft.AspNetCore": "Warning" } }