From 35034add2b7b019e422c846631430170b24deb05 Mon Sep 17 00:00:00 2001 From: vas Date: Fri, 18 Dec 2020 13:48:21 +0000 Subject: [PATCH 1/6] CON-2886 --- .../HomeControllerTests/WhenGettingIndex.cs | 3 ++- .../Controllers/ErrorController.cs | 3 ++- .../Controllers/HomeController.cs | 20 +++++++++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs index 05ad39c9c..b17ec22c5 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using AutoFixture; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using Moq; using NUnit.Framework; using SFA.DAS.CommitmentsV2.Shared.Interfaces; @@ -45,7 +46,7 @@ public WhenGettingIndexTestFixture() ModelMapper.Setup(x => x.Map(It.Is(r => r == Request))) .ReturnsAsync(ViewModel); - Controller = new HomeController(ModelMapper.Object); + Controller = new HomeController(ModelMapper.Object, Mock.Of>()); } public async Task GetIndex() diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs index f5b9b7009..789a6d09b 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs @@ -7,11 +7,12 @@ public class ErrorController : Controller [Route("error")] public IActionResult Error(int? statusCode) { - ViewBag.HideNav = true; + ViewBag.HideNav = true; switch (statusCode) { case 400: + case 401: case 403: case 404: return View(statusCode.ToString()); diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs index 455b7016b..049a786b7 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs @@ -1,5 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using SFA.DAS.Authorization.EmployerUserRoles.Options; using SFA.DAS.Authorization.Mvc.Attributes; using SFA.DAS.CommitmentsV2.Shared.Interfaces; @@ -12,16 +14,26 @@ namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers public class HomeController : Controller { private readonly IModelMapper _modelMapper; + private readonly ILogger _logger; - public HomeController(IModelMapper modelMapper) + public HomeController(IModelMapper modelMapper, ILogger logger) { _modelMapper = modelMapper; + _logger = logger; } public async Task Index(IndexRequest request) { - var model = await _modelMapper.Map(request); - return View(model); + try + { + var model = await _modelMapper.Map(request); + return View(model); + } + catch (Exception ex) + { + _logger.LogError($"Failed '{nameof(HomeController)}': {nameof(ex.Message)}='{ex.Message}', {nameof(ex.StackTrace)}='{ex.StackTrace}'"); + return RedirectToAction("Error", "Error"); + } } } } \ No newline at end of file From 34794f5023c516d0b3ba9a7551b160df35cb4e00 Mon Sep 17 00:00:00 2001 From: vas Date: Mon, 21 Dec 2020 08:39:47 +0000 Subject: [PATCH 2/6] CON-2886 --- .../HomeControllerTests/WhenGettingIndex.cs | 2 +- .../Controllers/ErrorController.cs | 8 +++++--- .../Controllers/HomeController.cs | 17 +++-------------- .../Startup/AspNetStartup.cs | 2 +- .../Startup/ErrorPagesStartup.cs | 12 +++++------- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs index b17ec22c5..9cb5d5f22 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs @@ -46,7 +46,7 @@ public WhenGettingIndexTestFixture() ModelMapper.Setup(x => x.Map(It.Is(r => r == Request))) .ReturnsAsync(ViewModel); - Controller = new HomeController(ModelMapper.Object, Mock.Of>()); + Controller = new HomeController(ModelMapper.Object); } public async Task GetIndex() diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs index 789a6d09b..b0542acb8 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers @@ -5,14 +6,15 @@ namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers public class ErrorController : Controller { [Route("error")] + [Route("error-local-development")] public IActionResult Error(int? statusCode) { - ViewBag.HideNav = true; + ViewBag.HideNav = true; + switch (statusCode) { - case 400: - case 401: + case 400: case 403: case 404: return View(statusCode.ToString()); diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs index 049a786b7..ec8aa2ca2 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using SFA.DAS.Authorization.EmployerUserRoles.Options; using SFA.DAS.Authorization.Mvc.Attributes; using SFA.DAS.CommitmentsV2.Shared.Interfaces; @@ -14,26 +13,16 @@ namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers public class HomeController : Controller { private readonly IModelMapper _modelMapper; - private readonly ILogger _logger; - public HomeController(IModelMapper modelMapper, ILogger logger) + public HomeController(IModelMapper modelMapper) { _modelMapper = modelMapper; - _logger = logger; } public async Task Index(IndexRequest request) { - try - { - var model = await _modelMapper.Map(request); - return View(model); - } - catch (Exception ex) - { - _logger.LogError($"Failed '{nameof(HomeController)}': {nameof(ex.Message)}='{ex.Message}', {nameof(ex.StackTrace)}='{ex.StackTrace}'"); - return RedirectToAction("Error", "Error"); - } + var model = await _modelMapper.Map(request); + return View(model); } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/AspNetStartup.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/AspNetStartup.cs index fd35af089..76db38f15 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/AspNetStartup.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/AspNetStartup.cs @@ -42,7 +42,7 @@ public void ConfigureContainer(Registry registry) public void Configure(IApplicationBuilder app) { - app.UseDasErrorPages() + app.UseDasErrorPages(Environment) .UseUnauthorizedAccessExceptionHandler() .UseHttpsRedirection() .UseDasHsts() diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs index 490594c0c..3637773ac 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs @@ -1,23 +1,21 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; namespace SFA.DAS.EmployerCommitmentsV2.Web.Startup { public static class ErrorPagesStartup { - public static IApplicationBuilder UseDasErrorPages(this IApplicationBuilder app) + public static IApplicationBuilder UseDasErrorPages(this IApplicationBuilder app, IHostingEnvironment environment) { - var hostingEnvironment = app.ApplicationServices.GetService(); - - if (hostingEnvironment.IsDevelopment()) + if (environment.IsDevelopment()) { - app.UseDeveloperExceptionPage(); + app.UseExceptionHandler("/error-local-development") + .UseStatusCodePagesWithReExecute("/error-local-development", "?statuscode={0}"); } else { app.UseExceptionHandler("/error") - .UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); + .UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); } return app; From 0c1223f8c9a88efe78c032d2f4b667a28df56b94 Mon Sep 17 00:00:00 2001 From: vas Date: Tue, 22 Dec 2020 09:24:59 +0000 Subject: [PATCH 3/6] CON-2886 --- .../HomeControllerTests/WhenGettingIndex.cs | 1 - .../Controllers/ErrorController.cs | 10 +++---- .../Startup/ErrorPagesStartup.cs | 27 ++++++++++++------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs index 9cb5d5f22..05ad39c9c 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/HomeControllerTests/WhenGettingIndex.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using AutoFixture; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using Moq; using NUnit.Framework; using SFA.DAS.CommitmentsV2.Shared.Interfaces; diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs index b0542acb8..215cd66d4 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs @@ -5,18 +5,16 @@ namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers { public class ErrorController : Controller { - [Route("error")] - [Route("error-local-development")] + [Route("error")] public IActionResult Error(int? statusCode) { - ViewBag.HideNav = true; - + ViewBag.HideNav = true; switch (statusCode) { - case 400: + case 400: case 403: - case 404: + case 404: return View(statusCode.ToString()); default: return View(); diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs index 3637773ac..9e82ba4c0 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs @@ -7,16 +7,23 @@ public static class ErrorPagesStartup { public static IApplicationBuilder UseDasErrorPages(this IApplicationBuilder app, IHostingEnvironment environment) { - if (environment.IsDevelopment()) - { - app.UseExceptionHandler("/error-local-development") - .UseStatusCodePagesWithReExecute("/error-local-development", "?statuscode={0}"); - } - else - { - app.UseExceptionHandler("/error") - .UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); - } + //if (environment.IsDevelopment()) + //{ + // app.UseExceptionHandler("/error-local-development"); + // app.UseHsts(); + // //.UseStatusCodePagesWithRedirects("{0}"); -- option1 + // //.UseStatusCodePagesWithReExecute("/error-local-development", "?statuscode={0}"); + //} + //else + //{ + // app.UseExceptionHandler("/error"); + // app.UseHsts(); + // //.UseStatusCodePagesWithRedirects("{0}"); + // //.UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); + //} + + app.UseExceptionHandler("/error"); + app.UseStatusCodePagesWithRedirects("~/error/?statuscode={0}"); return app; } From c381df07c8908d74f131dd76bf2fe6e3fcca2920 Mon Sep 17 00:00:00 2001 From: vas Date: Tue, 22 Dec 2020 09:32:21 +0000 Subject: [PATCH 4/6] CON-2886 : UseStatusCodePagesWithRedirects --- .../Startup/ErrorPagesStartup.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs index 9e82ba4c0..492dddfa4 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs @@ -9,17 +9,12 @@ public static IApplicationBuilder UseDasErrorPages(this IApplicationBuilder app, { //if (environment.IsDevelopment()) //{ - // app.UseExceptionHandler("/error-local-development"); - // app.UseHsts(); - // //.UseStatusCodePagesWithRedirects("{0}"); -- option1 - // //.UseStatusCodePagesWithReExecute("/error-local-development", "?statuscode={0}"); + // app.UseDeveloperExceptionPage(); //} //else //{ - // app.UseExceptionHandler("/error"); - // app.UseHsts(); - // //.UseStatusCodePagesWithRedirects("{0}"); - // //.UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); + // app.UseExceptionHandler("/error") + // .UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); //} app.UseExceptionHandler("/error"); From c36b3f24a38709bbba437db254a71a927e8e5efb Mon Sep 17 00:00:00 2001 From: vas Date: Tue, 22 Dec 2020 10:25:36 +0000 Subject: [PATCH 5/6] CON-2886 --- .../Controllers/ErrorController.cs | 9 ++++----- .../Startup/ErrorPagesStartup.cs | 20 +++++++++---------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs index 215cd66d4..f5b9b7009 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ErrorController.cs @@ -1,20 +1,19 @@ -using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Mvc; namespace SFA.DAS.EmployerCommitmentsV2.Web.Controllers { public class ErrorController : Controller { - [Route("error")] + [Route("error")] public IActionResult Error(int? statusCode) { - ViewBag.HideNav = true; + ViewBag.HideNav = true; switch (statusCode) { - case 400: + case 400: case 403: - case 404: + case 404: return View(statusCode.ToString()); default: return View(); diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs index 492dddfa4..30cde8dc1 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Startup/ErrorPagesStartup.cs @@ -7,18 +7,16 @@ public static class ErrorPagesStartup { public static IApplicationBuilder UseDasErrorPages(this IApplicationBuilder app, IHostingEnvironment environment) { - //if (environment.IsDevelopment()) - //{ - // app.UseDeveloperExceptionPage(); - //} - //else - //{ - // app.UseExceptionHandler("/error") - // .UseStatusCodePagesWithReExecute("/error", "?statuscode={0}"); - //} - app.UseExceptionHandler("/error"); - app.UseStatusCodePagesWithRedirects("~/error/?statuscode={0}"); + if (environment.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/error"); + app.UseStatusCodePagesWithRedirects("~/error/?statuscode={0}"); + } return app; } From d208708d74e7ca6590d3cfafaeccfde7ffb20943 Mon Sep 17 00:00:00 2001 From: vas Date: Tue, 22 Dec 2020 10:57:03 +0000 Subject: [PATCH 6/6] CON-2886 --- .../Controllers/HomeController.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs index ec8aa2ca2..455b7016b 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/HomeController.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using SFA.DAS.Authorization.EmployerUserRoles.Options; using SFA.DAS.Authorization.Mvc.Attributes;