Skip to content

Commit 36fb20a

Browse files
committed
Refactoring Catalog and Order status and add problem details feature from 2.1
1 parent b02c1b8 commit 36fb20a

4 files changed

Lines changed: 223 additions & 157 deletions

File tree

src/Services/Catalog/Catalog.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Catalog.API.Infrastructure.ActionResults;
22
using Catalog.API.Infrastructure.Exceptions;
33
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Http;
45
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.AspNetCore.Mvc.Filters;
67
using Microsoft.Extensions.Logging;
@@ -27,12 +28,16 @@ public void OnException(ExceptionContext context)
2728

2829
if (context.Exception.GetType() == typeof(CatalogDomainException))
2930
{
30-
var json = new JsonErrorResponse
31+
var problemDetails = new ValidationProblemDetails()
3132
{
32-
Messages = new[] { context.Exception.Message }
33+
Instance = context.HttpContext.Request.Path,
34+
Status = StatusCodes.Status400BadRequest,
35+
Detail = "Please refer to the errors property for additional details."
3336
};
3437

35-
context.Result = new BadRequestObjectResult(json);
38+
problemDetails.Errors.Add("DomainValidations", new string[] { context.Exception.Message.ToString() });
39+
40+
context.Result = new BadRequestObjectResult(problemDetails);
3641
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
3742
}
3843
else

src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries;
66
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
77
using Ordering.API.Application.Commands;
8-
using Ordering.API.Application.Models;
98
using System;
109
using System.Collections.Generic;
1110
using System.Net;
@@ -15,6 +14,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
1514
{
1615
[Route("api/v1/[controller]")]
1716
[Authorize]
17+
[ApiController]
1818
public class OrdersController : Controller
1919
{
2020
private readonly IMediator _mediator;

src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AspNetCore.Mvc;
44
using global::Ordering.Domain.Exceptions;
55
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Mvc.Filters;
78
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults;
89
using Microsoft.Extensions.Logging;
@@ -25,16 +26,18 @@ public void OnException(ExceptionContext context)
2526
context.Exception,
2627
context.Exception.Message);
2728

28-
if (context.Exception.GetType() == typeof(OrderingDomainException))
29+
if (context.Exception.GetType() == typeof(OrderingDomainException))
2930
{
30-
var json = new JsonErrorResponse
31+
var problemDetails = new ValidationProblemDetails()
3132
{
32-
Messages = new[] { context.Exception.Message }
33+
Instance = context.HttpContext.Request.Path,
34+
Status = StatusCodes.Status400BadRequest,
35+
Detail = "Please refer to the errors property for additional details."
3336
};
3437

35-
// Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
36-
//It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
37-
context.Result = new BadRequestObjectResult(json);
38+
problemDetails.Errors.Add("DomainValidations", new string[] { context.Exception.Message.ToString() });
39+
40+
context.Result = new BadRequestObjectResult(problemDetails);
3841
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
3942
}
4043
else
@@ -52,7 +55,7 @@ public void OnException(ExceptionContext context)
5255
// Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
5356
// It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
5457
context.Result = new InternalServerErrorObjectResult(json);
55-
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
58+
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
5659
}
5760
context.ExceptionHandled = true;
5861
}

0 commit comments

Comments
 (0)