From 3a7a14bdb718a42f2136b9b48063e0f28a6d7ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Tom=C3=A1s?= Date: Mon, 27 Mar 2017 14:05:28 +0200 Subject: [PATCH 1/2] Created global filters for web apis Fix bug BadRequest response after creating order --- .../InternalServerErrorObjectResult.cs | 18 +++++ .../Exceptions/BasketDomainException.cs | 24 +++++++ .../Filters/HttpGlobalExceptionFilter.cs | 67 +++++++++++++++++++ src/Services/Basket/Basket.API/Startup.cs | 7 +- .../InternalServerErrorObjectResult.cs | 18 +++++ .../Exceptions/CatalogDomainException.cs | 24 +++++++ .../Filters/HttpGlobalExceptionFilter.cs | 63 +++++++++++++++++ src/Services/Catalog/Catalog.API/Startup.cs | 16 ++--- .../Commands/CreateOrderCommandHandler.cs | 5 +- .../Commands/IdentifierCommandHandler.cs | 4 +- .../Filters/HttpGlobalExceptionFilter.cs | 9 ++- src/Services/Ordering/Ordering.API/Startup.cs | 9 +-- .../BuyerAggregate/PaymentMethod.cs | 9 +-- .../OrderAggregate/OrderItem.cs | 9 +-- .../OrderAggregate/OrderStatus.cs | 5 +- .../Exceptions/OrderingDomainException.cs | 23 +++++++ .../Ordering.Domain/SeedWork/IUnitOfWork.cs | 2 +- .../OrderingContext.cs | 4 +- .../Repositories/RequestManager.cs | 3 +- src/Web/WebMVC/Services/BasketService.cs | 2 + src/Web/WebMVC/Services/OrderingService.cs | 2 + .../Utilities/HttpApiClientWrapper.cs | 20 ++++-- 22 files changed, 297 insertions(+), 46 deletions(-) create mode 100644 src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs create mode 100644 src/Services/Basket/Basket.API/Infrastructure/Exceptions/BasketDomainException.cs create mode 100644 src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs create mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs create mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/Exceptions/CatalogDomainException.cs create mode 100644 src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs create mode 100644 src/Services/Ordering/Ordering.Domain/Exceptions/OrderingDomainException.cs diff --git a/src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs new file mode 100644 index 000000000..2ec3727a6 --- /dev/null +++ b/src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Basket.API.Infrastructure.ActionResults +{ + public class InternalServerErrorObjectResult : ObjectResult + { + public InternalServerErrorObjectResult(object error) + : base(error) + { + StatusCode = StatusCodes.Status500InternalServerError; + } + } +} diff --git a/src/Services/Basket/Basket.API/Infrastructure/Exceptions/BasketDomainException.cs b/src/Services/Basket/Basket.API/Infrastructure/Exceptions/BasketDomainException.cs new file mode 100644 index 000000000..199409ecc --- /dev/null +++ b/src/Services/Basket/Basket.API/Infrastructure/Exceptions/BasketDomainException.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Basket.API.Infrastructure.Exceptions +{ + /// + /// Exception type for app exceptions + /// + public class BasketDomainException : Exception + { + public BasketDomainException() + { } + + public BasketDomainException(string message) + : base(message) + { } + + public BasketDomainException(string message, Exception innerException) + : base(message, innerException) + { } + } +} diff --git a/src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs new file mode 100644 index 000000000..5acd0bbdc --- /dev/null +++ b/src/Services/Basket/Basket.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs @@ -0,0 +1,67 @@ +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; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading.Tasks; + +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 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/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index db72792bd..39c5755c8 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -14,6 +14,7 @@ using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events; using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using System; +using Basket.API.Infrastructure.Filters; namespace Microsoft.eShopOnContainers.Services.Basket.API { @@ -35,7 +36,11 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API public void ConfigureServices(IServiceCollection services) { // Add framework services. - services.AddMvc(); + services.AddMvc(options => + { + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + }).AddControllersAsServices(); + services.Configure(Configuration); //By connecting here we are making sure that our service diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs new file mode 100644 index 000000000..3ac3d0f78 --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +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/Exceptions/CatalogDomainException.cs b/src/Services/Catalog/Catalog.API/Infrastructure/Exceptions/CatalogDomainException.cs new file mode 100644 index 000000000..0b27131cf --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Infrastructure/Exceptions/CatalogDomainException.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Catalog.API.Infrastructure.Exceptions +{ + /// + /// Exception type for app exceptions + /// + public class CatalogDomainException : Exception + { + public CatalogDomainException() + { } + + public CatalogDomainException(string message) + : base(message) + { } + + public CatalogDomainException(string message, Exception innerException) + : base(message, innerException) + { } + } +} diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs new file mode 100644 index 000000000..e618d3eed --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs @@ -0,0 +1,63 @@ +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 1598eb5a0..2a7e84b2f 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -1,5 +1,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API { + using global::Catalog.API.Infrastructure.Filters; using global::Catalog.API.IntegrationEvents; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -41,6 +42,12 @@ { var sqlConnection = new SqlConnection(Configuration["ConnectionString"]); + // Add framework services. + services.AddMvc(options => + { + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + }).AddControllersAsServices(); + services.AddDbContext(c => { c.UseSqlServer(sqlConnection); @@ -85,20 +92,13 @@ var serviceProvider = services.BuildServiceProvider(); var configuration = serviceProvider.GetRequiredService>().Value; - services.AddSingleton(new EventBusRabbitMQ(configuration.EventBusConnection)); - - services.AddMvc(); + services.AddSingleton(new EventBusRabbitMQ(configuration.EventBusConnection)); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IntegrationEventLogContext integrationEventLogContext) { //Configure logs - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs index 6e8ebb381..2132d2983 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs @@ -51,11 +51,8 @@ _orderRepository.Add(order); - var result = await _orderRepository.UnitOfWork + return await _orderRepository.UnitOfWork .SaveEntitiesAsync(); - - return result > 0; - } } } diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/IdentifierCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/IdentifierCommandHandler.cs index 5fdc0181b..60a48ae89 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/IdentifierCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/IdentifierCommandHandler.cs @@ -48,9 +48,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands return CreateResultForDuplicateRequest(); } else - { - await _requestManager.CreateRequestForCommandAsync(message.Id); + { var result = await _mediator.SendAsync(message.Command); + await _requestManager.CreateRequestForCommandAsync(message.Id); return result; } } diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs index 3d181cd0c..25cf46830 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs @@ -1,11 +1,12 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Filters { using AspNetCore.Mvc; + using global::Ordering.Domain.Exceptions; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults; using Microsoft.Extensions.Logging; - using System; + using System.Net; public class HttpGlobalExceptionFilter : IExceptionFilter { @@ -24,7 +25,7 @@ context.Exception, context.Exception.Message); - if (context.Exception.GetType() == typeof(ArgumentException)) //TODO:Select a common exception for application like EshopException + if (context.Exception.GetType() == typeof(OrderingDomainException)) { var json = new JsonErrorResponse { @@ -32,6 +33,7 @@ }; context.Result = new BadRequestObjectResult(json); + context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; } else { @@ -46,8 +48,9 @@ } context.Result = new InternalServerErrorObjectResult(json); + context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } - + context.ExceptionHandled = true; } private class JsonErrorResponse diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 3f0840b07..1c419fc0f 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -45,7 +45,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 @@ -102,12 +102,7 @@ { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - + app.UseCors("CorsPolicy"); app.UseFailingMiddleware(); diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs index 0b1e2ceb0..2920ee2d9 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs @@ -1,4 +1,5 @@ using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; +using Ordering.Domain.Exceptions; using System; namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate @@ -22,13 +23,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B public PaymentMethod(int cardTypeId, string alias, string cardNumber, string securityNumber, string cardHolderName, DateTime expiration) { - _cardNumber = !string.IsNullOrWhiteSpace(cardNumber) ? cardNumber : throw new ArgumentException(nameof(cardNumber)); - _securityNumber = !string.IsNullOrWhiteSpace(securityNumber) ? securityNumber : throw new ArgumentException(nameof(securityNumber)); - _cardHolderName = !string.IsNullOrWhiteSpace(cardHolderName) ? cardHolderName : throw new ArgumentException(nameof(cardHolderName)); + _cardNumber = !string.IsNullOrWhiteSpace(cardNumber) ? cardNumber : throw new OrderingDomainException(nameof(cardNumber)); + _securityNumber = !string.IsNullOrWhiteSpace(securityNumber) ? securityNumber : throw new OrderingDomainException(nameof(securityNumber)); + _cardHolderName = !string.IsNullOrWhiteSpace(cardHolderName) ? cardHolderName : throw new OrderingDomainException(nameof(cardHolderName)); if (expiration < DateTime.UtcNow) { - throw new ArgumentException(nameof(expiration)); + throw new OrderingDomainException(nameof(expiration)); } _alias = alias; diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs index 22c7cc93c..9376da971 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderItem.cs @@ -1,4 +1,5 @@ using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; +using Ordering.Domain.Exceptions; using System; namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate @@ -24,12 +25,12 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O { if (units <= 0) { - throw new ArgumentNullException("Invalid number of units"); + throw new OrderingDomainException("Invalid number of units"); } if ((unitPrice * units) < discount) { - throw new ArgumentException("The total of order item is lower than applied discount"); + throw new OrderingDomainException("The total of order item is lower than applied discount"); } ProductId = productId; @@ -58,7 +59,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O { if (discount < 0) { - throw new ArgumentException("Discount is not valid"); + throw new OrderingDomainException("Discount is not valid"); } _discount = discount; @@ -68,7 +69,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O { if (units < 0) { - throw new ArgumentException("Invalid units"); + throw new OrderingDomainException("Invalid units"); } _units += units; diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderStatus.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderStatus.cs index cb4d863f9..6e3ff74b1 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderStatus.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderStatus.cs @@ -1,5 +1,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate { + using global::Ordering.Domain.Exceptions; using Seedwork; using SeedWork; using System; @@ -34,7 +35,7 @@ if (state == null) { - throw new ArgumentException($"Possible values for OrderStatus: {String.Join(",", List().Select(s => s.Name))}"); + throw new OrderingDomainException($"Possible values for OrderStatus: {String.Join(",", List().Select(s => s.Name))}"); } return state; @@ -46,7 +47,7 @@ if (state == null) { - throw new ArgumentException($"Possible values for OrderStatus: {String.Join(",", List().Select(s => s.Name))}"); + throw new OrderingDomainException($"Possible values for OrderStatus: {String.Join(",", List().Select(s => s.Name))}"); } return state; diff --git a/src/Services/Ordering/Ordering.Domain/Exceptions/OrderingDomainException.cs b/src/Services/Ordering/Ordering.Domain/Exceptions/OrderingDomainException.cs new file mode 100644 index 000000000..7a7320dbf --- /dev/null +++ b/src/Services/Ordering/Ordering.Domain/Exceptions/OrderingDomainException.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Ordering.Domain.Exceptions +{ + /// + /// Exception type for domain exceptions + /// + public class OrderingDomainException : Exception + { + public OrderingDomainException() + { } + + public OrderingDomainException(string message) + : base(message) + { } + + public OrderingDomainException(string message, Exception innerException) + : base(message, innerException) + { } + } +} diff --git a/src/Services/Ordering/Ordering.Domain/SeedWork/IUnitOfWork.cs b/src/Services/Ordering/Ordering.Domain/SeedWork/IUnitOfWork.cs index 581f905a6..810f0b2af 100644 --- a/src/Services/Ordering/Ordering.Domain/SeedWork/IUnitOfWork.cs +++ b/src/Services/Ordering/Ordering.Domain/SeedWork/IUnitOfWork.cs @@ -7,6 +7,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork public interface IUnitOfWork : IDisposable { Task SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken)); - Task SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)); + Task SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)); } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs b/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs index b74c7631d..f9f68cfb3 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs @@ -235,7 +235,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure .IsRequired(); } - public async Task SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)) + public async Task SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)) { // Dispatch Domain Events collection. // Choices: @@ -249,7 +249,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure // After executing this line all the changes performed thought the DbContext will be commited var result = await base.SaveChangesAsync(); - return result; + return true; } } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/RequestManager.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/RequestManager.cs index 1661ab6e5..a6d795214 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/RequestManager.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/RequestManager.cs @@ -1,4 +1,5 @@ using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; +using Ordering.Domain.Exceptions; using System; using System.Collections.Generic; using System.Text; @@ -26,7 +27,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor var exists = await ExistAsync(id); var request = exists ? - throw new Exception($"Request with {id} already exists") : + throw new OrderingDomainException($"Request with {id} already exists") : new ClientRequest() { Id = id, diff --git a/src/Web/WebMVC/Services/BasketService.cs b/src/Web/WebMVC/Services/BasketService.cs index 0e6b13ab5..d9fc23854 100644 --- a/src/Web/WebMVC/Services/BasketService.cs +++ b/src/Web/WebMVC/Services/BasketService.cs @@ -56,6 +56,8 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services var response = await _apiClient.PostAsync(basketUrl, basket); + response.EnsureSuccessStatusCode(); + return basket; } diff --git a/src/Web/WebMVC/Services/OrderingService.cs b/src/Web/WebMVC/Services/OrderingService.cs index d53db3090..06c388a3b 100644 --- a/src/Web/WebMVC/Services/OrderingService.cs +++ b/src/Web/WebMVC/Services/OrderingService.cs @@ -88,6 +88,8 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) throw new Exception("Error creating order, try later"); + + response.EnsureSuccessStatusCode(); } public void OverrideUserInfoIntoOrder(Order original, Order destination) diff --git a/src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs b/src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs index 06172eb53..6904e2d7a 100644 --- a/src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs +++ b/src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs @@ -1,8 +1,10 @@ -using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Polly; using Polly.Wrap; using System; +using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -76,13 +78,17 @@ namespace WebMVC.Services.Utilities // a new StringContent must be created for each retry // as it is disposed after each call HttpInvoker(() => + { + var response = _client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json")); + // raise exception if HttpResponseCode 500 + // needed for circuit breaker to track fails + if (response.Result.StatusCode == HttpStatusCode.InternalServerError) { - var response = _client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json")); - // raise exception if not success response - // needed for circuit breaker to track fails - response.Result.EnsureSuccessStatusCode(); - return response; - }); + throw new HttpRequestException(); + } + + return response; + }); public Task DeleteAsync(string uri) => HttpInvoker(() => _client.DeleteAsync(uri)); From 2119ae19bd192012dda80ccd9ce4229deda3227e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Tom=C3=A1s?= Date: Mon, 27 Mar 2017 15:24:29 +0200 Subject: [PATCH 2/2] Fix startup after merge --- src/Services/Catalog/Catalog.API/Startup.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 92e7a658c..bfdba9d61 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -1,7 +1,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API { using global::Catalog.API.Infrastructure.Filters; - using global::Catalog.API.IntegrationEvents; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; @@ -17,7 +16,6 @@ using Microsoft.Extensions.Options; using System; using System.Data.Common; - using System.Data.SqlClient; using System.Reflection; public class Startup @@ -43,14 +41,13 @@ public void ConfigureServices(IServiceCollection services) { - // Add framework services. services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); - services.AddDbContext(c => + services.AddDbContext(options => { options.UseSqlServer(Configuration["ConnectionString"], sqlServerOptionsAction: sqlOptions =>