using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; namespace TarninaHub.Errors; public 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, "Unhandled exception for {Method} {Path}", httpContext.Request.Method, httpContext.Request.Path); var problem = new ProblemDetails { Type = "https://httpstatuses.io/500", Title = "An unexpected error occurred.", Status = StatusCodes.Status500InternalServerError, Instance = httpContext.Request.Path, }; httpContext.Response.StatusCode = problem.Status.Value; await httpContext.Response.WriteAsJsonAsync(problem, cancellationToken); return true; } }