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 7ebe47b44..bfdba9d61 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 Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
@@ -15,7 +16,6 @@
using Microsoft.Extensions.Options;
using System;
using System.Data.Common;
- using System.Data.SqlClient;
using System.Reflection;
public class Startup
@@ -41,6 +41,12 @@
public void ConfigureServices(IServiceCollection services)
{
+ // Add framework services.
+ services.AddMvc(options =>
+ {
+ options.Filters.Add(typeof(HttpGlobalExceptionFilter));
+ }).AddControllersAsServices();
+
services.AddDbContext(options =>
{
options.UseSqlServer(Configuration["ConnectionString"],
@@ -86,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)
{
//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 92edf1974..f90c9bb98 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
@@ -106,12 +106,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 dcf7c54e9..3870ad470 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:
@@ -250,7 +250,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
// 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));