diff --git a/JixMinApi/Features/Todo/TodoEndpoints.cs b/JixMinApi/Features/Todo/TodoEndpoints.cs index b1a361b..5c95cd1 100644 --- a/JixMinApi/Features/Todo/TodoEndpoints.cs +++ b/JixMinApi/Features/Todo/TodoEndpoints.cs @@ -10,12 +10,12 @@ namespace JixMinApi.Features.Todo; public static class TodoEndpoints { - public static void InjectTodoEndpointServices(this IServiceCollection services) + public static void AddTodoEndpointServices(this IServiceCollection services) { services.AddDbContext(opt => opt.UseInMemoryDatabase(Constants.TodoApiGroupName)); } - public static void MapTodoEndpoints(this WebApplication app) + public static void UseTodoEndpoints(this WebApplication app) { var group = app.MapGroup(Constants.TodoApiRootPath) .WithOpenApi(x => new OpenApiOperation(x) diff --git a/JixMinApi/Program.cs b/JixMinApi/Program.cs index 425afd4..8d64c08 100644 --- a/JixMinApi/Program.cs +++ b/JixMinApi/Program.cs @@ -1,4 +1,5 @@ using JixMinApi.Features.Todo; +using JixMinApi.Shared; using Microsoft.OpenApi.Models; using System.Reflection; @@ -23,7 +24,10 @@ builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly)); // Inject endpoint services -builder.Services.InjectTodoEndpointServices(); +builder.Services.AddTodoEndpointServices(); + +builder.Services.AddExceptionHandler(); +builder.Services.AddProblemDetails(); var app = builder.Build(); @@ -35,8 +39,7 @@ } app.UseHttpsRedirection(); - -// Map Endpoints -app.MapTodoEndpoints(); +app.UseExceptionHandler(); +app.UseTodoEndpoints(); app.Run(); \ No newline at end of file diff --git a/JixMinApi/Shared/GlobalExceptionHandler.cs b/JixMinApi/Shared/GlobalExceptionHandler.cs new file mode 100644 index 0000000..8b24867 --- /dev/null +++ b/JixMinApi/Shared/GlobalExceptionHandler.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.Mvc; + +namespace JixMinApi.Shared; + +internal sealed class GlobalExceptionHandler : IExceptionHandler +{ + private readonly ILogger _logger; + + public GlobalExceptionHandler(ILogger logger) + { + _logger = logger; + } + + public async ValueTask TryHandleAsync( + HttpContext httpContext, + Exception exception, + CancellationToken cancellationToken) + { + _logger.LogError( + exception, "Exception occurred: {Message}", exception.Message); + + var problemDetails = new ProblemDetails + { + Status = StatusCodes.Status500InternalServerError, + Title = "Server error" + }; + + httpContext.Response.StatusCode = problemDetails.Status.Value; + + await httpContext.Response + .WriteAsJsonAsync(problemDetails, cancellationToken); + + return true; + } +} diff --git a/JixMinApi/Shared/Result.cs b/JixMinApi/Shared/Result.cs deleted file mode 100644 index 53b3b24..0000000 --- a/JixMinApi/Shared/Result.cs +++ /dev/null @@ -1,41 +0,0 @@ -namespace JixMinApi.Shared; - - -public class Result -{ - public bool IsSuccess { get; init; } - public T? Value { get; init; } - - public bool IsError { get; init; } - public Exception? Exception { get; init; } - - public bool HasValidationError { get; init; } - public List> ValidationErrors { get; init; } = new List>(); - - - public Result(T value) - { - Value = value; - IsSuccess = true; - } - - public Result(Exception exception) - { - Exception = exception; - IsError = true; - } - - public Result(List> validationErrors) - { - ValidationErrors = validationErrors; - HasValidationError = true; - } - - public Result(string field, string validationErrorMessage) - { - var validationErrors = new List>(); - validationErrors.Add(new KeyValuePair(field, validationErrorMessage)); - ValidationErrors = validationErrors; - HasValidationError = true; - } -} \ No newline at end of file