AltHeroes Bot v2
1using Microsoft.AspNetCore.Diagnostics;
2using Microsoft.AspNetCore.Http;
3using Microsoft.AspNetCore.Mvc;
4using Microsoft.Extensions.Logging;
5
6namespace AltBot.ServiceDefaults;
7
8internal sealed class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
9{
10 public async ValueTask<bool> TryHandleAsync(
11 HttpContext httpContext,
12 Exception exception,
13 CancellationToken cancellationToken)
14 {
15 logger.LogCritical(exception, "Unhandled exception occurred: {Message}", exception.Message);
16
17 var problemDetails = new ProblemDetails
18 {
19 Status = StatusCodes.Status500InternalServerError,
20 Title = "Server error"
21 };
22
23 httpContext.Response.StatusCode = problemDetails.Status.Value;
24
25 await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
26
27 return true;
28 }
29}