Global error handler for ASP.NET Core application

I am trying to configure a global error handler for my web app.

Configuration
In the Startup.cs Configure method:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseMiddleware<ErrorHandlerMiddleware>();
}

Middleware

public class ErrorHandlerMiddleware
{
    // RequestDelegate denotes a HTTP Request completion.
    private readonly RequestDelegate _next;
    public ErrorHandlerMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task Invoke(HttpContext context)
    {
        try
        {
            await _next(context);
        }
        catch (Exception error)
        {
            var response = context.Response;
            response.ContentType = "application/json";
            // Creates an ErrorReponse Model out of the error message using the Fail method
            var responseModel = ErrorResponse<string>.Fail(error.Message);
            switch (error)
            {
                case ExceptionService e:
                    // custom application error
                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                    break;
                case KeyNotFoundException e:
                    // not found error
                    response.StatusCode = (int)HttpStatusCode.NotFound;
                    break;
                default:
                    // unhandled error
                    response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    break;
            }
            var result = JsonSerializer.Serialize(responseModel);

            // Create a new ITempDataDictionary object
            var tempData = context.RequestServices.GetRequiredService<ITempDataProvider>().LoadTempData(context);

            // Add key-value pairs to the temp data dictionary
            //tempData["ErrorMessage"] = "An unexpected error occurred."; 
            
            tempData["Error_Message"] = error.Message.ToString();
            tempData["Error_StackTrace"] = error.StackTrace.ToString();
            tempData["Error_Source"] = error.Source.ToString();
            tempData["Error_TargetSite"] = error.TargetSite.ToString();

            if (error.InnerException != null)
                tempData["Error_InnerException"] = error.InnerException.ToString();

            // Save the temp data dictionary to the HttpContext
            context.RequestServices.GetRequiredService<ITempDataProvider>().SaveTempData(context, tempData);
            //context.Response.Redirect(result);
            context.Response.Redirect($"/Error?code={response.StatusCode}");
        }
    }

}

Error Controller

public class ErrorController : Controller
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly IEmailService _emailService;
    private readonly AppConfiguration _appConfig;
    private readonly AppMailSettings _mailSettings;

    public ErrorController(IUnitOfWork unitOfWork,
                            UserManager<ApplicationUser> userManager, 
                            IEmailService emailService,
                            IOptions<AppConfiguration> appConfig,
                            IOptions<AppMailSettings> mailSettings)
    {
        _unitOfWork = unitOfWork;
        _userManager = userManager;
        _emailService = emailService;
        _appConfig = appConfig.Value;
        _mailSettings = mailSettings.Value;
    }

    [AllowAnonymous]
    [Route("Error")]
    public async Task<IActionResult> Error(string code)
    {
        var Error_Message = TempData["Error_Message"];
        var Error_StackTrace = TempData["Error_StackTrace"];
        var Error_Source = TempData["Error_Source"] ;
        var Error_TargetSite = TempData["Error_TargetSite"];

        ErrorViewModel model = new ErrorViewModel();

        model.ErrorMessage = "Sorry, an application error occured. Our support team has been notified.";

        model.ErrorDateTime = DateTime.Now.ToString();
        model.StackTrace = Error_StackTrace.ToString();
        model.InnerException = TempData["Error_Message"].ToString();
        model.Source = Error_Source.ToString();
        model.TargetSite = Error_TargetSite.ToString();

        var user = await _userManager.GetUserAsync(HttpContext.User);
        var baseUrl = "http://localhost:44350";

        if (user != null)
        {
            model.UserId = user.Id;
            model.UserName = user.UserName;
            model.Role = user.UserType;
            
            var mailRequest = new MailRequest();
            //mailRequest.ToAddresses = new List<string> { "[email protected]", _mailSettings.ErrorEmailAddress };
            mailRequest.ToAddresses = new List<string> { "[email protected]", _mailSettings.ErrorEmailAddress };
            mailRequest.Subject = "WFC Plus Error Report";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"User: {model.UserName}");
            // other details in string builder
            sb.AppendLine($"Is Development: {model.IsDevelopment}");
            mailRequest.Body = sb.ToString();
            await _emailService.SendEmail(mailRequest);

        }
        return View(model);
    }
}

Problem
Invoke method of ErrorHandlerMiddleware is getting hit on startup so I know that the environment setting is correct, but exceptions that I am triggering in testing are not getting handled by the middleware.

I am not sure how to proceed.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật