| 123456789101112131415161718192021222324252627 |
- using Microsoft.AspNetCore.Diagnostics;
- using Microsoft.AspNetCore.Mvc;
- namespace Web.Api.Common;
- internal sealed class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
- {
- public async ValueTask<bool> 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;
- }
- }
|