| 1234567891011121314151617181920212223242526 |
- using Microsoft.AspNetCore.Diagnostics;
- 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");
- httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
- httpContext.Response.ContentType = "application/json";
- await httpContext.Response.WriteAsJsonAsync(new ApiResponse
- {
- Success = false,
- Status = StatusCodes.Status500InternalServerError,
- Message = "Server failure"
- }, cancellationToken);
- return true;
- }
- }
|