Skip to content

Commit 1bd1374

Browse files
committed
Add exceptions filters
1 parent a5aea54 commit 1bd1374

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.ActionResults
2+
{
3+
using AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
public class InternalServerErrorObjectResult : ObjectResult
7+
{
8+
public InternalServerErrorObjectResult(object error)
9+
: base(error)
10+
{
11+
StatusCode = StatusCodes.Status500InternalServerError;
12+
}
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Exception type for app exceptions
7+
/// </summary>
8+
public class MarketingDomainException : Exception
9+
{
10+
public MarketingDomainException()
11+
{ }
12+
13+
public MarketingDomainException(string message)
14+
: base(message)
15+
{ }
16+
17+
public MarketingDomainException(string message, Exception innerException)
18+
: base(message, innerException)
19+
{ }
20+
}
21+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filters
2+
{
3+
using AspNetCore.Mvc;
4+
using global::Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Mvc.Filters;
7+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.ActionResults;
8+
using Microsoft.Extensions.Logging;
9+
using System.Net;
10+
11+
public class HttpGlobalExceptionFilter : IExceptionFilter
12+
{
13+
private readonly IHostingEnvironment env;
14+
private readonly ILogger<HttpGlobalExceptionFilter> logger;
15+
16+
public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger<HttpGlobalExceptionFilter> logger)
17+
{
18+
this.env = env;
19+
this.logger = logger;
20+
}
21+
22+
public void OnException(ExceptionContext context)
23+
{
24+
logger.LogError(new EventId(context.Exception.HResult),
25+
context.Exception,
26+
context.Exception.Message);
27+
28+
if (context.Exception.GetType() == typeof(MarketingDomainException))
29+
{
30+
var json = new JsonErrorResponse
31+
{
32+
Messages = new[] { context.Exception.Message }
33+
};
34+
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+
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
39+
}
40+
else
41+
{
42+
var json = new JsonErrorResponse
43+
{
44+
Messages = new[] { "An error occur.Try it again." }
45+
};
46+
47+
if (env.IsDevelopment())
48+
{
49+
json.DeveloperMessage = context.Exception;
50+
}
51+
52+
// Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
53+
// It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
54+
context.Result = new InternalServerErrorObjectResult(json);
55+
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
56+
}
57+
context.ExceptionHandled = true;
58+
}
59+
60+
private class JsonErrorResponse
61+
{
62+
public string[] Messages { get; set; }
63+
64+
public object DeveloperMessage { get; set; }
65+
}
66+
}
67+
}

src/Services/Marketing/Marketing.API/Startup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Extensions.Logging;
1111
using System.Reflection;
1212
using System;
13+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filters;
1314

1415
public class Startup
1516
{
@@ -37,7 +38,10 @@ public Startup(IHostingEnvironment env)
3738
public void ConfigureServices(IServiceCollection services)
3839
{
3940
// Add framework services.
40-
services.AddMvc();
41+
services.AddMvc(options =>
42+
{
43+
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
44+
}).AddControllersAsServices(); //Injecting Controllers themselves thru DIFor further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
4145

4246
services.AddDbContext<MarketingContext>(options =>
4347
{

0 commit comments

Comments
 (0)