Skip to content
This repository was archived by the owner on Dec 2, 2023. It is now read-only.

Commit 35fed67

Browse files
committed
Updated startup configuration to be fluent
1 parent 09eb5c2 commit 35fed67

File tree

12 files changed

+177
-123
lines changed

12 files changed

+177
-123
lines changed

ExceptionAll.APIExample/Controllers/WeatherForecastController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger,
3030
[HttpGet]
3131
public async Task<IActionResult> GetAll()
3232
{
33+
await Task.Delay(0);
3334
var rng = new Random();
3435
var result = Enumerable.Range(1, 5).Select(index => new WeatherForecast
3536
{
@@ -39,7 +40,6 @@ public async Task<IActionResult> GetAll()
3940
})
4041
.ToArray();
4142
throw new Exception("This is simulating an uncaught exception");
42-
return Ok(result);
4343
}
4444

4545
// If the developer doesn't want to use the template in all instances,
@@ -48,6 +48,7 @@ public async Task<IActionResult> GetAll()
4848
[Route("api.GetWithoutExceptionAllError")]
4949
public async Task<IActionResult> GetWithoutExceptionAllError()
5050
{
51+
await Task.Delay(0);
5152
try
5253
{
5354
throw new Exception("Some exception");
@@ -65,6 +66,7 @@ public async Task<IActionResult> GetWithoutExceptionAllError()
6566
[Route("api/GetSomething")]
6667
public async Task<IActionResult> GetSomethingWithQuery([FromQuery]string test)
6768
{
69+
await Task.Delay(0);
6870
return _actionResultService.GetResponse<NotFoundDetails>(ControllerContext,
6971
$"No item exists with name of {test}");
7072
}

ExceptionAll.APIExample/ExceptionAll.APIExample.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="FluentValidation" Version="10.2.3" />
9-
<PackageReference Include="FluentValidation.AspNetCore" Version="10.2.3" />
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
8+
<PackageReference Include="FluentValidation" Version="10.3.0" />
9+
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.0" />
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

ExceptionAll.APIExample/Startup.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,14 @@ public void Configure(IApplicationBuilder app,
4949
IErrorResponseService errorResponseService,
5050
IActionResultService actionResultService)
5151
{
52-
errorResponseService.AddErrorResponse(new ErrorResponse
53-
{
54-
ErrorTitle = "Bad Request - Data Annotations",
55-
ExceptionType = typeof(System.ComponentModel.DataAnnotations.ValidationException),
56-
DetailsType = typeof(BadRequestDetails),
57-
LogAction = (e) => actionResultService
58-
.Logger
59-
.LogDebug(e, e.Message)
60-
});
61-
62-
errorResponseService.AddErrorResponse(new ErrorResponse
63-
{
64-
ErrorTitle = "Bad Request - Fluent Validation",
65-
ExceptionType = typeof(FluentValidation.ValidationException),
66-
DetailsType = typeof(BadRequestDetails),
67-
LogAction = (e) => actionResultService
68-
.Logger
69-
.LogDebug(e, e.Message)
70-
});
52+
errorResponseService.AddErrorResponse(
53+
ErrorResponse
54+
.CreateErrorResponse(actionResultService)
55+
.WithTitle("Bad Request - Fluent Validation")
56+
.ForException(typeof(FluentValidation.ValidationException))
57+
.WithReturnType(typeof(BadRequestDetails))
58+
.WithLogAction((x, e) => x.LogError("Something bad happened", e))
59+
);
7160

7261
// Adds CorrelationId to incoming requests for tracking. Optional
7362
app.UseCorrelationIdMiddleware();

ExceptionAll.Tests/Dtos/ErrorResponseTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Collections;
1+
using ExceptionAll.Dtos;
2+
using ExceptionAll.Validation;
23
using System.Collections.Generic;
34
using System.Linq;
4-
using ExceptionAll.Dtos;
5-
using ExceptionAll.Validation;
65
using Xunit;
76
using Xunit.Abstractions;
87

@@ -48,14 +47,16 @@ public void ErrorResponse_ShouldNotBeValid(ErrorResponse response)
4847
Assert.True(!test.IsValid);
4948
}
5049

51-
public static IEnumerable<object[]> GetValidErrorResponses()
50+
public static IEnumerable<object[]> GetValidErrorResponses()
5251
{
53-
return TestHelper.GetValidErrorResponses().ToList();
52+
var mockActionResultService = TestHelper.GetMockActionResultService();
53+
return TestHelper.GetValidErrorResponses(mockActionResultService.Object).ToList();
5454
}
5555

5656
public static IEnumerable<object[]> GetInvalidErrorResponses()
5757
{
58-
return TestHelper.GetInvalidErrorResponses().ToList();
58+
var mockActionResultService = TestHelper.GetMockActionResultService();
59+
return TestHelper.GetInvalidErrorResponses(mockActionResultService.Object).ToList();
5960
}
6061
}
6162
}

ExceptionAll.Tests/Services/ErrorResponseServiceTests.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@ public void AddErrorResponse_ShouldSuccessfullyAdd(ErrorResponse response)
4141
public void AddErrorResponses_ShouldSuccessfullyAdd()
4242
{
4343
// Arrange
44-
var mockLogger = new Mock<ILogger<IErrorResponseService>>();
45-
var mockService = new Mock<ErrorResponseService>(mockLogger.Object);
44+
var mockErsLogger = new Mock<ILogger<IErrorResponseService>>();
45+
var mockErrorResponseService = new Mock<ErrorResponseService>(mockErsLogger.Object);
46+
var mockActionResultService = TestHelper.GetMockActionResultService();
47+
4648
var responses = new List<ErrorResponse>
4749
{
48-
new()
49-
{
50-
ExceptionType = typeof(OperationCanceledException),
51-
DetailsType = typeof(NotFoundDetails)
52-
},
53-
54-
new()
55-
{
56-
ExceptionType = typeof(ValidationException),
57-
DetailsType = typeof(NotFoundDetails)
58-
},
50+
ErrorResponse
51+
.CreateErrorResponse(mockActionResultService.Object)
52+
.ForException(typeof(OperationCanceledException))
53+
.WithReturnType(typeof(NotFoundDetails))
54+
.WithLogAction(null),
55+
56+
ErrorResponse
57+
.CreateErrorResponse(mockActionResultService.Object)
58+
.ForException(typeof(ValidationException))
59+
.WithReturnType(typeof(NotFoundDetails))
60+
.WithLogAction(null)
5961
};
6062

6163
// Act
6264
foreach (var response in responses)
6365
{
64-
mockService.Object.AddErrorResponse(response);
66+
mockErrorResponseService.Object.AddErrorResponse(response);
6567
}
6668

6769
// Assess
6870
TestOutputHelper.WriteLine($"Error Responses: {responses.ToJson()}");
6971

7072
// Assert
71-
Assert.NotNull(mockService.Object.GetErrorResponses());
72-
Assert.NotEmpty(mockService.Object.GetErrorResponses());
73+
Assert.NotNull(mockErrorResponseService.Object.GetErrorResponses());
74+
Assert.NotEmpty(mockErrorResponseService.Object.GetErrorResponses());
7375
}
7476

7577
[Fact]
@@ -78,11 +80,11 @@ public void AddErrorResponse_ShouldThrow()
7880
// Arrange
7981
var mockLogger = new Mock<ILogger<IErrorResponseService>>();
8082
var mockService = new Mock<ErrorResponseService>(mockLogger.Object);
81-
var response = new ErrorResponse()
82-
{
83-
ExceptionType = typeof(OperationCanceledException),
84-
DetailsType = typeof(NotFoundDetails)
85-
};
83+
var mockActionResultService = TestHelper.GetMockActionResultService();
84+
var response = ErrorResponse
85+
.CreateErrorResponse(mockActionResultService.Object)
86+
.ForException(typeof(OperationCanceledException))
87+
.WithReturnType(typeof(NotFoundDetails));
8688

8789
// Act
8890
mockService.Object.AddErrorResponse(response);
@@ -97,7 +99,8 @@ public void AddErrorResponse_ShouldThrow()
9799

98100
public static IEnumerable<object[]> GetValidErrorResponses()
99101
{
100-
return TestHelper.GetValidErrorResponses().ToList();
102+
var mockActionResultService = TestHelper.GetMockActionResultService();
103+
return TestHelper.GetValidErrorResponses(mockActionResultService.Object).ToList();
101104
}
102105
}
103106
}

ExceptionAll.Tests/TestHelper.cs

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -71,83 +71,70 @@ public static DefaultHttpContext GetMockHttpContext()
7171

7272
public static Mock<ActionResultService>GetMockActionResultService()
7373
{
74+
var mockErrorLogger = new Mock<ILogger<IErrorResponseService>>();
7475
var mockActionLogger = new Mock<ILogger<IActionResultService>>();
75-
var mockErrorResponseService = new Mock<ErrorResponseService>();
76+
var mockErrorResponseService = new Mock<ErrorResponseService>(mockErrorLogger.Object);
7677
return new Mock<ActionResultService>(
7778
mockActionLogger.Object,
7879
mockErrorResponseService.Object);
7980
}
8081

81-
public static IEnumerable<object[]> GetValidErrorResponses()
82+
public static IEnumerable<object[]> GetValidErrorResponses(IActionResultService actionResultService)
8283
{
8384
return new List<object[]>
8485
{
8586
// Every property populated
86-
new object[]{new ErrorResponse
87-
{
88-
ErrorTitle = "Test",
89-
ExceptionType = typeof(Exception),
90-
DetailsType = typeof(BadRequestDetails),
91-
LogAction = (x) => new Mock<ActionResultService>().Object.Logger.LogDebug(x, "Test")
92-
}},
87+
new object[]{
88+
ErrorResponse
89+
.CreateErrorResponse(actionResultService)
90+
.WithTitle("Bad Request - Fluent Validation")
91+
.ForException(typeof(ValidationException))
92+
.WithReturnType(typeof(BadRequestDetails))
93+
.WithLogAction((x, e) => x.LogError("Something bad happened", e))
94+
},
9395

9496
// No title
95-
new object[]{new ErrorResponse
96-
{
97-
ExceptionType = typeof(Exception),
98-
DetailsType = typeof(BadRequestDetails),
99-
LogAction = (x) => new Mock<ActionResultService>().Object.Logger.LogDebug(x, "Test")
100-
}},
97+
new object[]{
98+
ErrorResponse
99+
.CreateErrorResponse(actionResultService)
100+
.ForException(typeof(ValidationException))
101+
.WithReturnType(typeof(BadRequestDetails))
102+
.WithLogAction((x, e) => x.LogError("Something bad happened", e))
103+
},
101104

102105
// No log action
103-
new object[]{new ErrorResponse
104-
{
105-
ErrorTitle = "Test",
106-
ExceptionType = typeof(Exception),
107-
DetailsType = typeof(BadRequestDetails)
108-
}},
106+
new object[]{
107+
ErrorResponse
108+
.CreateErrorResponse(actionResultService)
109+
.WithTitle("Bad Request - Fluent Validation")
110+
.ForException(typeof(ValidationException))
111+
.WithReturnType(typeof(BadRequestDetails))
112+
},
109113

110114
// Only details type
111-
new object[]{new ErrorResponse
112-
{
113-
DetailsType = typeof(NotFoundDetails)
114-
}},
115-
116-
new object[]{new ErrorResponse
117-
{
118-
ExceptionType = typeof(ArgumentException),
119-
DetailsType = typeof(NotFoundDetails)
120-
}},
121-
122-
new object[]{new ErrorResponse
123-
{
124-
ExceptionType = typeof(ArgumentNullException),
125-
DetailsType = typeof(NotFoundDetails)
126-
}},
127-
128-
new object[]{new ErrorResponse
129-
{
130-
ExceptionType = typeof(OperationCanceledException),
131-
DetailsType = typeof(NotFoundDetails)
132-
}},
133-
134-
new object[]{new ErrorResponse
135-
{
136-
ExceptionType = typeof(ValidationException),
137-
DetailsType = typeof(NotFoundDetails)
138-
}},
115+
new object[]{
116+
ErrorResponse
117+
.CreateErrorResponse(actionResultService)
118+
.WithReturnType(typeof(BadRequestDetails))
119+
},
120+
121+
// Only creation method
122+
new object[]{
123+
ErrorResponse.CreateErrorResponse(actionResultService)
124+
},
139125
};
140126
}
141127

142-
public static IEnumerable<object[]> GetInvalidErrorResponses()
128+
public static IEnumerable<object[]> GetInvalidErrorResponses(IActionResultService actionResultService)
143129
{
144130
return new List<object[]>
145131
{
146-
new object[]{new ErrorResponse
147-
{
148-
ExceptionType = typeof(string),
149-
DetailsType = typeof(ErrorResponse),
150-
}},
132+
new object[]{
133+
ErrorResponse
134+
.CreateErrorResponse(actionResultService)
135+
.ForException(typeof(string))
136+
.WithReturnType(typeof(ErrorResponse))
137+
}
151138
};
152139
}
153140
}

0 commit comments

Comments
 (0)