Skip to content

Commit f641f0b

Browse files
authored
Release v0.7.1
* Adds ExceptionMiddleware
1 parent d53e0db commit f641f0b

4 files changed

Lines changed: 118 additions & 32 deletions

File tree

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using FinancialHub.Core.Infra.Logs.Middlewares;
2-
using Microsoft.AspNetCore.Builder;
3-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
42

53
namespace FinancialHub.Core.Infra.Logs.Extensions.Configurations
64
{
@@ -11,10 +9,5 @@ public static IServiceCollection AddCoreLogging(this IServiceCollection services
119
services.AddLogging();
1210
return services;
1311
}
14-
15-
public static IApplicationBuilder UseLogRequest(this IApplicationBuilder builder)
16-
{
17-
return builder.UseMiddleware<LogMiddleware>();
18-
}
1912
}
2013
}

src/api/core/FinancialHub.Core.Infra.Logs/Middlewares/LogMiddleware.cs renamed to src/api/core/FinancialHub.Core.WebApi/Middlewares/ExceptionMiddleware.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.Extensions.Logging;
33

4-
namespace FinancialHub.Core.Infra.Logs.Middlewares
4+
namespace FinancialHub.Core.WebApi.Middlewares
55
{
6-
public class LogMiddleware
6+
public class ExceptionMiddleware
77
{
88
private readonly RequestDelegate next;
9-
private readonly ILogger<LogMiddleware> logger;
9+
private readonly ILogger<ExceptionMiddleware> logger;
1010

11-
public LogMiddleware(RequestDelegate next, ILogger<LogMiddleware> logger)
11+
public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger)
1212
{
1313
this.next = next;
1414
this.logger = logger;
@@ -25,13 +25,23 @@ public async Task InvokeAsync(HttpContext context)
2525
}
2626
catch (Exception exception)
2727
{
28-
var status = context.Response.StatusCode;
2928
this.logger.LogError(
3029
exception,
31-
"[{request}] - {path} error with message {message} and status {status}",
32-
method, path, exception.Message, status
30+
"[{request}] - {path} error with message {message}",
31+
method, path, exception.Message
32+
);
33+
context.Response.StatusCode = 500;
34+
await context.Response.WriteAsJsonAsync(
35+
new
36+
{
37+
HasError = true,
38+
Error = new
39+
{
40+
Code = 500,
41+
exception.Message,
42+
}
43+
}
3344
);
34-
throw;
3545
}
3646
finally
3747
{

src/api/core/FinancialHub.Core.WebApi/Startup.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using FinancialHub.Core.Resources.Extensions;
1010
using FinancialHub.Core.Infra.Data.Extensions.Configurations;
1111
using FinancialHub.Core.Infra.Logs.Extensions.Configurations;
12+
using FinancialHub.Core.WebApi.Middlewares;
1213

1314
namespace FinancialHub.Core.WebApi
1415
{
@@ -23,21 +24,19 @@ public Startup(IConfiguration configuration)
2324

2425
public void ConfigureServices(IServiceCollection services)
2526
{
26-
services.AddApiConfigurations();
27-
services.AddApiDocs();
28-
services.AddApiLogging();
29-
30-
services.AddHttpLogging(logging =>
31-
{
32-
logging.LoggingFields = Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All;
33-
});
34-
35-
services.AddCoreResources();
36-
services.AddCoreServices();
37-
services.AddCoreInfra();
38-
services.AddRepositories(Configuration);
39-
40-
services.AddMvc().AddNewtonsoftJson();
27+
services
28+
.AddApiConfigurations()
29+
.AddApiDocs()
30+
.AddApiLogging();
31+
32+
services.AddCoreResources()
33+
.AddCoreServices()
34+
.AddCoreInfra()
35+
.AddRepositories(Configuration);
36+
37+
services
38+
.AddMvc()
39+
.AddNewtonsoftJson();
4140
}
4241

4342
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
@@ -50,7 +49,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5049
}
5150

5251
app.UseRouting();
53-
app.UseLogRequest();
52+
app.UseMiddleware<ExceptionMiddleware>();
5453
app.UseEndpoints(endpoints =>
5554
{
5655
endpoints.MapControllers();
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using FinancialHub.Core.WebApi.Middlewares;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.Extensions.Logging;
4+
using System.IO;
5+
using System.Text.Json;
6+
7+
namespace FinancialHub.Core.WebApi.Tests.Middleware
8+
{
9+
public class ExceptionMiddlewareTests
10+
{
11+
private ILogger<ExceptionMiddleware> logger;
12+
13+
[SetUp]
14+
public void Setup()
15+
{
16+
this.logger = new Mock<ILogger<ExceptionMiddleware>>().Object;
17+
}
18+
19+
[Test]
20+
public void InvokeAsync_SuccessRequest_ShouldNotThrowException()
21+
{
22+
static Task next(HttpContext hc) => Task.CompletedTask;
23+
var middleware = new ExceptionMiddleware(next, this.logger);
24+
var context = new DefaultHttpContext();
25+
26+
Assert.That(
27+
async () => await middleware.InvokeAsync(context),
28+
Throws.Nothing
29+
);
30+
}
31+
32+
[Test]
33+
public async Task InvokeAsync_SuccessRequest_ShouldNotChangePayload()
34+
{
35+
static Task next(HttpContext hc) => Task.CompletedTask;
36+
37+
var middleware = new ExceptionMiddleware(next, this.logger);
38+
39+
var response = new Mock<HttpResponse>();
40+
response
41+
.SetupGet(x => x.StatusCode)
42+
.Returns(200);
43+
var context = new Mock<HttpContext>();
44+
context
45+
.SetupGet(x => x.Response)
46+
.Returns(response.Object);
47+
context
48+
.SetupGet(x => x.Request)
49+
.Returns(new DefaultHttpContext().Request);
50+
51+
var defaultContext = new DefaultHttpContext();
52+
53+
await middleware.InvokeAsync(context.Object);
54+
55+
Assert.That(response.Object.StatusCode, Is.EqualTo(200));
56+
}
57+
58+
[Test]
59+
public void InvokeAsync_RequestWithUnhandledError_ShouldNotThrowException()
60+
{
61+
static Task next(HttpContext hc) => Task.CompletedTask;
62+
var middleware = new ExceptionMiddleware(next, this.logger);
63+
var context = new DefaultHttpContext();
64+
65+
Assert.That(
66+
async () => await middleware.InvokeAsync(context),
67+
Throws.Nothing
68+
);
69+
}
70+
71+
[Test]
72+
public async Task InvokeAsync_RequestWithUnhandledError_ShouldChangeStatusTo500()
73+
{
74+
static Task next(HttpContext hc) => Task.FromException(new Exception("Error"));
75+
76+
var middleware = new ExceptionMiddleware(next, this.logger);
77+
var context = new DefaultHttpContext();
78+
79+
await middleware.InvokeAsync(context);
80+
81+
Assert.That(context.Response.StatusCode, Is.EqualTo(500));
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)