From a399c78c3458adfba5b9bf6076141b35a55d7a62 Mon Sep 17 00:00:00 2001 From: IgorSychev Date: Thu, 22 Jun 2017 18:05:40 +0300 Subject: [PATCH] DRY https://en.wikipedia.org/wiki/Don%27t_repeat_yourself in web.common project extracted classes copy-pasted in all api projects. --- eShopOnContainers-ServicesAndWebApps.sln | 51 ++++++++++++++ .../InternalServerErrorObjectResult.cs | 2 +- .../Filters/HttpGlobalExceptionFilter.cs | 15 ++--- .../Web.Common/Web.Common.csproj | 13 ++++ .../Basket/Basket.API/Basket.API.csproj | 1 + .../Filters/HttpGlobalExceptionFilter.cs | 63 ----------------- src/Services/Basket/Basket.API/Startup.cs | 6 +- .../Catalog/Catalog.API/Catalog.API.csproj | 1 + .../InternalServerErrorObjectResult.cs | 14 ---- .../Filters/HttpGlobalExceptionFilter.cs | 63 ----------------- src/Services/Catalog/Catalog.API/Startup.cs | 5 +- .../InternalServerErrorObjectResult.cs | 14 ---- .../Filters/HttpGlobalExceptionFilter.cs | 67 ------------------- .../Locations.API/Locations.API.csproj | 1 + .../Location/Locations.API/Startup.cs | 5 +- .../InternalServerErrorObjectResult.cs | 14 ---- .../Filters/HttpGlobalExceptionFilter.cs | 67 ------------------- .../Marketing.API/Marketing.API.csproj | 1 + .../Marketing/Marketing.API/Startup.cs | 5 +- .../InternalServerErrorObjectResult.cs | 14 ---- .../Ordering/Ordering.API/Ordering.API.csproj | 1 + src/Services/Ordering/Ordering.API/Startup.cs | 8 +-- 22 files changed, 92 insertions(+), 339 deletions(-) rename src/{Services/Basket/Basket.API => BuildingBlocks/Web.Common}/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs (82%) rename src/{Services/Ordering/Ordering.API => BuildingBlocks/Web.Common}/Infrastructure/Filters/HttpGlobalExceptionFilter.cs (81%) create mode 100644 src/BuildingBlocks/Web.Common/Web.Common.csproj delete mode 100644 src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs delete mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs delete mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs delete mode 100644 src/Services/Location/Locations.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs delete mode 100644 src/Services/Location/Locations.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs delete mode 100644 src/Services/Marketing/Marketing.API/Infrastructure/ActionResult/InternalServerErrorObjectResult.cs delete mode 100644 src/Services/Marketing/Marketing.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs delete mode 100644 src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs diff --git a/eShopOnContainers-ServicesAndWebApps.sln b/eShopOnContainers-ServicesAndWebApps.sln index 0a36c0c57..6aef0d5e0 100644 --- a/eShopOnContainers-ServicesAndWebApps.sln +++ b/eShopOnContainers-ServicesAndWebApps.sln @@ -96,6 +96,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Marketing", "Marketing", "{ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Marketing.API", "src\Services\Marketing\Marketing.API\Marketing.API.csproj", "{DF395F85-B010-465D-857A-7EBCC512C0C2}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web.Common", "src\BuildingBlocks\Web.Common\Web.Common.csproj", "{003782EA-73EC-4330-ABAE-79E050A6F91E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Ad-Hoc|Any CPU = Ad-Hoc|Any CPU @@ -1262,6 +1264,54 @@ Global {DF395F85-B010-465D-857A-7EBCC512C0C2}.Release|x64.Build.0 = Release|Any CPU {DF395F85-B010-465D-857A-7EBCC512C0C2}.Release|x86.ActiveCfg = Release|Any CPU {DF395F85-B010-465D-857A-7EBCC512C0C2}.Release|x86.Build.0 = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|x64.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Ad-Hoc|x86.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|Any CPU.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|ARM.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|ARM.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|iPhone.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|iPhone.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|x64.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|x64.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|x86.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.AppStore|x86.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|ARM.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|ARM.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|iPhone.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|x64.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|x64.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|x86.ActiveCfg = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Debug|x86.Build.0 = Debug|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|Any CPU.Build.0 = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|ARM.ActiveCfg = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|ARM.Build.0 = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|iPhone.ActiveCfg = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|iPhone.Build.0 = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|x64.ActiveCfg = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|x64.Build.0 = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|x86.ActiveCfg = Release|Any CPU + {003782EA-73EC-4330-ABAE-79E050A6F91E}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1308,5 +1358,6 @@ Global {23A33F9B-7672-426D-ACF9-FF8436ADC81A} = {88B22DBB-AA8F-4290-A454-2C109352C345} {A5260DE0-1FDD-467E-9CC1-A028AB081CEE} = {91CF7717-08AB-4E65-B10E-0B426F01E2E8} {DF395F85-B010-465D-857A-7EBCC512C0C2} = {A5260DE0-1FDD-467E-9CC1-A028AB081CEE} + {003782EA-73EC-4330-ABAE-79E050A6F91E} = {DB0EFB20-B024-4E5E-A75C-52143C131D25} EndGlobalSection EndGlobal diff --git a/src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/BuildingBlocks/Web.Common/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs similarity index 82% rename from src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs rename to src/BuildingBlocks/Web.Common/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs index a0b988156..a163c8211 100644 --- a/src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs +++ b/src/BuildingBlocks/Web.Common/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -namespace Basket.API.Infrastructure.ActionResults +namespace Microsoft.eShopOnContainers.Infrastructure.ActionResults { public class InternalServerErrorObjectResult : ObjectResult { diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/BuildingBlocks/Web.Common/Infrastructure/Filters/HttpGlobalExceptionFilter.cs similarity index 81% rename from src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs rename to src/BuildingBlocks/Web.Common/Infrastructure/Filters/HttpGlobalExceptionFilter.cs index 69faf8e42..52f9febe8 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ b/src/BuildingBlocks/Web.Common/Infrastructure/Filters/HttpGlobalExceptionFilter.cs @@ -1,19 +1,18 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Filters +namespace Microsoft.eShopOnContainers.Infrastructure.Filters { - using AspNetCore.Mvc; - using global::Ordering.Domain.Exceptions; + using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Filters; - using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults; using Microsoft.Extensions.Logging; using System.Net; + using Microsoft.eShopOnContainers.Infrastructure.ActionResults; - public class HttpGlobalExceptionFilter : IExceptionFilter + public class HttpGlobalExceptionFilter : IExceptionFilter { private readonly IHostingEnvironment env; - private readonly ILogger logger; + private readonly ILogger> logger; - public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger logger) + public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger> logger) { this.env = env; this.logger = logger; @@ -25,7 +24,7 @@ context.Exception, context.Exception.Message); - if (context.Exception.GetType() == typeof(OrderingDomainException)) + if (context.Exception.GetType() == typeof(T)) { var json = new JsonErrorResponse { diff --git a/src/BuildingBlocks/Web.Common/Web.Common.csproj b/src/BuildingBlocks/Web.Common/Web.Common.csproj new file mode 100644 index 000000000..edeae100f --- /dev/null +++ b/src/BuildingBlocks/Web.Common/Web.Common.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp1.1 + + + + + + + + + \ No newline at end of file diff --git a/src/Services/Basket/Basket.API/Basket.API.csproj b/src/Services/Basket/Basket.API/Basket.API.csproj index 5287dcc96..0da5f0c9e 100644 --- a/src/Services/Basket/Basket.API/Basket.API.csproj +++ b/src/Services/Basket/Basket.API/Basket.API.csproj @@ -41,6 +41,7 @@ + diff --git a/src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs deleted file mode 100644 index ab7989973..000000000 --- a/src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Basket.API.Infrastructure.ActionResults; -using Basket.API.Infrastructure.Exceptions; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Logging; -using System.Net; - -namespace Basket.API.Infrastructure.Filters -{ - public class HttpGlobalExceptionFilter : IExceptionFilter - { - private readonly IHostingEnvironment env; - private readonly ILogger logger; - - public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger logger) - { - this.env = env; - this.logger = logger; - } - - public void OnException(ExceptionContext context) - { - logger.LogError(new EventId(context.Exception.HResult), - context.Exception, - context.Exception.Message); - - if (context.Exception.GetType() == typeof(BasketDomainException)) - { - var json = new JsonErrorResponse - { - Messages = new[] { context.Exception.Message } - }; - - context.Result = new BadRequestObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; - } - else - { - var json = new JsonErrorResponse - { - Messages = new[] { "An error occurred. Try it again." } - }; - - if (env.IsDevelopment()) - { - json.DeveloperMessage = context.Exception; - } - - context.Result = new InternalServerErrorObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; - } - context.ExceptionHandled = true; - } - - private class JsonErrorResponse - { - public string[] Messages { get; set; } - - public object DeveloperMessage { get; set; } - } - } -} diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index 83b259d5b..5a2ea2cf4 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -1,6 +1,6 @@ using Autofac; using Autofac.Extensions.DependencyInjection; -using Basket.API.Infrastructure.Filters; +using Basket.API.Infrastructure.Exceptions; using Basket.API.IntegrationEvents.EventHandling; using Basket.API.IntegrationEvents.Events; using Microsoft.AspNetCore.Builder; @@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; -using Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server; +using Microsoft.eShopOnContainers.Infrastructure.Filters; using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling; using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events; using Microsoft.eShopOnContainers.Services.Basket.API.Model; @@ -54,7 +54,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API // Add framework services. services.AddMvc(options => { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); services.Configure(Configuration); diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index 8f30f7ca3..0e2bdab46 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -63,6 +63,7 @@ + diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs deleted file mode 100644 index a6138b476..000000000 --- a/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace Catalog.API.Infrastructure.ActionResults -{ - public class InternalServerErrorObjectResult : ObjectResult - { - public InternalServerErrorObjectResult(object error) - : base(error) - { - StatusCode = StatusCodes.Status500InternalServerError; - } - } -} diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs deleted file mode 100644 index e618d3eed..000000000 --- a/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Catalog.API.Infrastructure.ActionResults; -using Catalog.API.Infrastructure.Exceptions; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Logging; -using System.Net; - -namespace Catalog.API.Infrastructure.Filters -{ - public class HttpGlobalExceptionFilter : IExceptionFilter - { - private readonly IHostingEnvironment env; - private readonly ILogger logger; - - public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger logger) - { - this.env = env; - this.logger = logger; - } - - public void OnException(ExceptionContext context) - { - logger.LogError(new EventId(context.Exception.HResult), - context.Exception, - context.Exception.Message); - - if (context.Exception.GetType() == typeof(CatalogDomainException)) - { - var json = new JsonErrorResponse - { - Messages = new[] { context.Exception.Message } - }; - - context.Result = new BadRequestObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; - } - else - { - var json = new JsonErrorResponse - { - Messages = new[] { "An error ocurr.Try it again." } - }; - - if (env.IsDevelopment()) - { - json.DeveloperMeesage = context.Exception; - } - - context.Result = new InternalServerErrorObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; - } - context.ExceptionHandled = true; - } - - private class JsonErrorResponse - { - public string[] Messages { get; set; } - - public object DeveloperMeesage { get; set; } - } - } -} diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index c5784b6a4..1e7809fbf 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -2,7 +2,7 @@ { using Autofac; using Autofac.Extensions.DependencyInjection; - using global::Catalog.API.Infrastructure.Filters; + using global::Catalog.API.Infrastructure.Exceptions; using global::Catalog.API.IntegrationEvents; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -13,6 +13,7 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; + using Microsoft.eShopOnContainers.Infrastructure.Filters; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling; using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events; @@ -66,7 +67,7 @@ services.AddMvc(options => { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); services.AddDbContext(options => diff --git a/src/Services/Location/Locations.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Location/Locations.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs deleted file mode 100644 index 772129da9..000000000 --- a/src/Services/Location/Locations.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.ActionResults -{ - public class InternalServerErrorObjectResult : ObjectResult - { - public InternalServerErrorObjectResult(object error) - : base(error) - { - StatusCode = StatusCodes.Status500InternalServerError; - } - } -} diff --git a/src/Services/Location/Locations.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Location/Locations.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs deleted file mode 100644 index 30b9df1fa..000000000 --- a/src/Services/Location/Locations.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ /dev/null @@ -1,67 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters -{ - using AspNetCore.Mvc; - using Microsoft.AspNetCore.Hosting; - using Microsoft.AspNetCore.Mvc.Filters; - using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.ActionResults; - using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions; - using Microsoft.Extensions.Logging; - using System.Net; - - public class HttpGlobalExceptionFilter : IExceptionFilter - { - private readonly IHostingEnvironment env; - private readonly ILogger logger; - - public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger logger) - { - this.env = env; - this.logger = logger; - } - - public void OnException(ExceptionContext context) - { - logger.LogError(new EventId(context.Exception.HResult), - context.Exception, - context.Exception.Message); - - if (context.Exception.GetType() == typeof(LocationDomainException)) - { - var json = new JsonErrorResponse - { - Messages = new[] { context.Exception.Message } - }; - - // Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1 - //It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information - context.Result = new BadRequestObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; - } - else - { - var json = new JsonErrorResponse - { - Messages = new[] { "An error occur.Try it again." } - }; - - if (env.IsDevelopment()) - { - json.DeveloperMessage = context.Exception; - } - - // Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1 - // It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information - context.Result = new InternalServerErrorObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; - } - context.ExceptionHandled = true; - } - - private class JsonErrorResponse - { - public string[] Messages { get; set; } - - public object DeveloperMessage { get; set; } - } - } -} diff --git a/src/Services/Location/Locations.API/Locations.API.csproj b/src/Services/Location/Locations.API/Locations.API.csproj index b9ef671dc..d6631fd17 100644 --- a/src/Services/Location/Locations.API/Locations.API.csproj +++ b/src/Services/Location/Locations.API/Locations.API.csproj @@ -41,6 +41,7 @@ + diff --git a/src/Services/Location/Locations.API/Startup.cs b/src/Services/Location/Locations.API/Startup.cs index f767f227b..f7b8da82e 100644 --- a/src/Services/Location/Locations.API/Startup.cs +++ b/src/Services/Location/Locations.API/Startup.cs @@ -7,7 +7,6 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure; -using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services; using Microsoft.Extensions.Configuration; @@ -16,6 +15,8 @@ using Microsoft.Extensions.Logging; using RabbitMQ.Client; using System.Reflection; using System; +using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions; +using Microsoft.eShopOnContainers.Infrastructure.Filters; namespace Microsoft.eShopOnContainers.Services.Locations.API { @@ -46,7 +47,7 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API // Add framework services. services.AddMvc(options => { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); services.Configure(Configuration); diff --git a/src/Services/Marketing/Marketing.API/Infrastructure/ActionResult/InternalServerErrorObjectResult.cs b/src/Services/Marketing/Marketing.API/Infrastructure/ActionResult/InternalServerErrorObjectResult.cs deleted file mode 100644 index 5975d92af..000000000 --- a/src/Services/Marketing/Marketing.API/Infrastructure/ActionResult/InternalServerErrorObjectResult.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.ActionResults -{ - using AspNetCore.Http; - using Microsoft.AspNetCore.Mvc; - - public class InternalServerErrorObjectResult : ObjectResult - { - public InternalServerErrorObjectResult(object error) - : base(error) - { - StatusCode = StatusCodes.Status500InternalServerError; - } - } -} \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Marketing/Marketing.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs deleted file mode 100644 index 14e79775d..000000000 --- a/src/Services/Marketing/Marketing.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ /dev/null @@ -1,67 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filters -{ - using AspNetCore.Mvc; - using global::Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions; - using Microsoft.AspNetCore.Hosting; - using Microsoft.AspNetCore.Mvc.Filters; - using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.ActionResults; - using Microsoft.Extensions.Logging; - using System.Net; - - public class HttpGlobalExceptionFilter : IExceptionFilter - { - private readonly IHostingEnvironment env; - private readonly ILogger logger; - - public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger logger) - { - this.env = env; - this.logger = logger; - } - - public void OnException(ExceptionContext context) - { - logger.LogError(new EventId(context.Exception.HResult), - context.Exception, - context.Exception.Message); - - if (context.Exception.GetType() == typeof(MarketingDomainException)) - { - var json = new JsonErrorResponse - { - Messages = new[] { context.Exception.Message } - }; - - // Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1 - //It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information - context.Result = new BadRequestObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; - } - else - { - var json = new JsonErrorResponse - { - Messages = new[] { "An error occur.Try it again." } - }; - - if (env.IsDevelopment()) - { - json.DeveloperMessage = context.Exception; - } - - // Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1 - // It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information - context.Result = new InternalServerErrorObjectResult(json); - context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; - } - context.ExceptionHandled = true; - } - - private class JsonErrorResponse - { - public string[] Messages { get; set; } - - public object DeveloperMessage { get; set; } - } - } -} \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/Marketing.API.csproj b/src/Services/Marketing/Marketing.API/Marketing.API.csproj index 4a6e2d4ea..4f77961e4 100644 --- a/src/Services/Marketing/Marketing.API/Marketing.API.csproj +++ b/src/Services/Marketing/Marketing.API/Marketing.API.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Services/Marketing/Marketing.API/Startup.cs b/src/Services/Marketing/Marketing.API/Startup.cs index a31055d39..9ade995eb 100644 --- a/src/Services/Marketing/Marketing.API/Startup.cs +++ b/src/Services/Marketing/Marketing.API/Startup.cs @@ -10,7 +10,6 @@ using Microsoft.Extensions.Logging; using System.Reflection; using System; - using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filters; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using RabbitMQ.Client; using BuildingBlocks.EventBus.Abstractions; @@ -23,6 +22,8 @@ using Polly; using System.Threading.Tasks; using System.Data.SqlClient; + using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions; + using Microsoft.eShopOnContainers.Infrastructure.Filters; public class Startup { @@ -52,7 +53,7 @@ // Add framework services. services.AddMvc(options => { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); //Injecting Controllers themselves thru DIFor further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services services.Configure(Configuration); diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs deleted file mode 100644 index 55cb33c99..000000000 --- a/src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults -{ - using AspNetCore.Http; - using Microsoft.AspNetCore.Mvc; - - public class InternalServerErrorObjectResult : ObjectResult - { - public InternalServerErrorObjectResult(object error) - : base(error) - { - StatusCode = StatusCodes.Status500InternalServerError; - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index 96b4f6627..fca3f777f 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -25,6 +25,7 @@ + diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 80b7b2abe..c5854f34f 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -4,13 +4,10 @@ using Autofac; using Autofac.Extensions.DependencyInjection; using global::Ordering.API.Application.IntegrationEvents; - using global::Ordering.API.Application.IntegrationEvents.EventHandling; using global::Ordering.API.Application.IntegrationEvents.Events; - using global::Ordering.API.Infrastructure.Middlewares; + using global::Ordering.Domain.Exceptions; using Infrastructure; - using Infrastructure.Auth; using Infrastructure.AutofacModules; - using Infrastructure.Filters; using Infrastructure.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -20,6 +17,7 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; + using Microsoft.eShopOnContainers.Infrastructure.Filters; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.HealthChecks; @@ -59,7 +57,7 @@ // Add framework services. services.AddMvc(options => { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); //Injecting Controllers themselves thru DI //For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services