Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat (ExceptionHandler) : Add Middleware #19

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Map.Api/Extension/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FluentValidation;
using Map.API.AutoMapperProfies;
using Map.API.Configuration;
using Map.API.Tools;
using Map.API.Validator.AuthValidator;
using Map.API.Validator.StepValidator;
using Map.API.Validator.TestimonialValidator;
Expand Down Expand Up @@ -37,6 +38,12 @@ public static void SetupConfiguration(this ConfigurationManager configuration)
.AddEnvironmentVariables();
}

public static void ConfigureExceptionHandler(this IServiceCollection services)
{
services.AddExceptionHandler<GlobalExceptionHandler>();
services.AddProblemDetails();
}


/// <summary>
/// Configures the swagger.
Expand Down
5 changes: 5 additions & 0 deletions Map.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

builder.Configuration.SetupConfiguration();

// Add Exception Handler
builder.Services.ConfigureExceptionHandler();

// Add Controllers to services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
Expand Down Expand Up @@ -61,6 +64,8 @@

app.UseAuthorization();

app.UseExceptionHandler();

app.MapControllers();

app.Run();
Expand Down
33 changes: 33 additions & 0 deletions Map.Api/Tools/GlobalExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace Map.API.Tools;

internal sealed class GlobalExceptionHandler : IExceptionHandler
{
private readonly ILogger<GlobalExceptionHandler> _logger;

public GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async ValueTask<bool> TryHandleAsync(HttpContext httpContext,
Exception exception,
CancellationToken cancellationToken)
{
_logger.LogError(exception, "An unhandled exception has occurred while executing the request.");
_logger.LogError(exception, "Exception occurred: {Message}", exception.Message);

ProblemDetails details = new()
{
Title = "Une erreur serveur s'est produite lors du traitement de votre demande.",
Status = StatusCodes.Status500InternalServerError,
};

httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
await httpContext.Response.WriteAsJsonAsync(details, cancellationToken);

return true;
}
}
Loading