using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; namespace Web.Api.Common; internal sealed class GlobalExceptionHandler(ILogger logger) : IExceptionHandler { public async ValueTask TryHandleAsync( HttpContext httpContext, Exception exception, CancellationToken cancellationToken) { logger.LogError(exception, "Unhandled exception occurred"); var problemDetails = new ProblemDetails { Status = StatusCodes.Status500InternalServerError, Type = "https://tools.ietf.org/html/rfc7231#section-6.6.1", Title = "Server failure" }; httpContext.Response.StatusCode = problemDetails.Status.Value; await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken); return true; } }