at main 1.1 kB view raw
1using AltBot.Core.Exceptions; 2using Microsoft.AspNetCore.Diagnostics; 3using Microsoft.AspNetCore.Http; 4using Microsoft.AspNetCore.Mvc; 5using Microsoft.Extensions.Logging; 6 7namespace AltBot.ServiceDefaults; 8 9internal sealed class HaltExceptionHandler(ILogger<HaltExceptionHandler> logger) : IExceptionHandler 10{ 11 public async ValueTask<bool> TryHandleAsync( 12 HttpContext httpContext, 13 Exception exception, 14 CancellationToken cancellationToken) 15 { 16 if (exception is not HaltException haltException) 17 { 18 return false; 19 } 20 21 logger.LogCritical(haltException, "AltBot crash: {Message}", haltException.Message); 22 23 var problemDetails = new ProblemDetails 24 { 25 Status = StatusCodes.Status500InternalServerError, 26 Title = "AltBot Halted", 27 Detail = haltException.Message 28 }; 29 30 httpContext.Response.StatusCode = problemDetails.Status.Value; 31 32 await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken); 33 34 return true; 35 } 36} 37