using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; namespace {{pascalCase PROJECT_NAME}}.Exceptions { public class CustomExceptionHandler : IExceptionHandler { public async ValueTask TryHandleAsync(HttpContext context, Exception exception, CancellationToken cancellationToken) { int statusCode = exception switch { UnauthorizedAccessException => StatusCodes.Status401Unauthorized, ArgumentException => StatusCodes.Status400BadRequest, _ => StatusCodes.Status500InternalServerError }; context.Response.StatusCode = statusCode; var problemDetails = new ProblemDetails { Status = statusCode, Title = "An error occurred", Type = exception.GetType().Name, Detail = exception.Message, Instance = context.Request.Path }; await context.Response.WriteAsJsonAsync(problemDetails, cancellationToken); return true; } } }