using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace AltBot.ServiceDefaults; internal sealed class GlobalExceptionHandler(ILogger logger) : IExceptionHandler { public async ValueTask TryHandleAsync( HttpContext httpContext, Exception exception, CancellationToken cancellationToken) { logger.LogCritical(exception, "Unhandled 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; } }