Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit 37fe200

Browse files
committed
style: Improve code style
1 parent 07ac0ed commit 37fe200

9 files changed

Lines changed: 41 additions & 53 deletions

File tree

src/ApiProblemDetailsFactory.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ internal sealed class ApiProblemDetailsFactory : ProblemDetailsFactory, IProblem
1919
public ApiProblemDetailsFactory(
2020
IHttpContextAccessor httpContextAccessor,
2121
IOptions<ApiBehaviorOptions> apiBehaviorOptions,
22-
IOptions<ErrorHandlerOptions> errorHandlerOptions
23-
)
22+
IOptions<ErrorHandlerOptions> errorHandlerOptions)
2423
{
2524
_httpContextAccessor = httpContextAccessor;
2625

@@ -51,8 +50,7 @@ public ErrorProblemDetails CreateProblem(
5150
int? statusCode = null,
5251
string title = null,
5352
string type = null,
54-
IDictionary<string, ErrorDetails> errors = null
55-
)
53+
IDictionary<string, ErrorDetails> errors = null)
5654
{
5755
errors ??= new Dictionary<string, ErrorDetails>();
5856

@@ -117,17 +115,15 @@ internal ErrorProblemDetails Create(ActionContext actionContext)
117115

118116
return Create(
119117
actionContext.HttpContext,
120-
payloadTooLargeError
121-
);
118+
payloadTooLargeError);
122119
}
123120

124121

125122
actionContext.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
126123

127124
return Create(
128125
actionContext.HttpContext,
129-
actionContext.ModelState.MappingModelState()
130-
);
126+
actionContext.ModelState.MappingModelState());
131127
}
132128

133129
public override ProblemDetails CreateProblemDetails(
@@ -136,8 +132,7 @@ public override ProblemDetails CreateProblemDetails(
136132
string title = null,
137133
string type = null,
138134
string detail = null,
139-
string instance = null
140-
)
135+
string instance = null)
141136
{
142137
var problemDetails = new ProblemDetails
143138
{
@@ -160,8 +155,7 @@ public override ValidationProblemDetails CreateValidationProblemDetails(
160155
string title = null,
161156
string type = null,
162157
string detail = null,
163-
string instance = null
164-
)
158+
string instance = null)
165159
{
166160
if(modelStateDictionary == null)
167161
{

src/ErrorHandlerExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public static IServiceCollection AddErrorHandler(this IServiceCollection service
2424
foreach(var details in ProblemDetailsDefaults.Defaults)
2525
{
2626
// Override existents
27-
if(options.ClientErrorMapping.ContainsKey(details.Key))
27+
if(options.ClientErrorMapping.TryGetValue(details.Key, out var value))
2828
{
29-
options.ClientErrorMapping[details.Key].Link = details.Value.Link;
30-
options.ClientErrorMapping[details.Key].Title = details.Value.Title;
29+
value.Link = details.Value.Link;
30+
value.Title = details.Value.Title;
3131
}
3232
else
3333
{
@@ -38,8 +38,7 @@ public static IServiceCollection AddErrorHandler(this IServiceCollection service
3838
{
3939
Link = details.Value.Link,
4040
Title = details.Value.Title
41-
}
42-
);
41+
});
4342
}
4443
}
4544
});

src/ErrorProblemDetails.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public class ErrorDetails
5858
public ErrorDetails() { }
5959
public ErrorDetails(
6060
string code,
61-
string description
62-
)
61+
string description)
6362
{
6463
Code = code;
6564
Description = description;

src/Handlers/ErrorHandlerMiddleware.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ internal class ErrorHandlerMiddleware
1515
public ErrorHandlerMiddleware(
1616
RequestDelegate next,
1717
ILogger<ErrorHandlerMiddleware> logger,
18-
ApiProblemDetailsFactory problemDetailsFactory
19-
)
18+
ApiProblemDetailsFactory problemDetailsFactory)
2019
{
2120
_next = next;
2221
_logger = logger;
@@ -26,6 +25,9 @@ ApiProblemDetailsFactory problemDetailsFactory
2625

2726
public async Task Invoke(HttpContext httpContext)
2827
{
28+
httpContext.Request.ContentLength = 0;
29+
30+
2931
await _next(httpContext);
3032

3133
if(httpContext.IsNotSuccess())

src/IProblemHandler.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ ObjectResult CreateProblemResult(
2121
int? statusCode = null,
2222
string title = null,
2323
string type = null,
24-
IDictionary<string, ErrorDetails> errors = null
25-
);
24+
IDictionary<string, ErrorDetails> errors = null);
2625

2726
/// <summary>
2827
/// Produces a <see cref="ErrorProblemDetails"/> response.
@@ -40,7 +39,6 @@ ErrorProblemDetails CreateProblem(
4039
int? statusCode = null,
4140
string title = null,
4241
string type = null,
43-
IDictionary<string, ErrorDetails> errors = null
44-
);
42+
IDictionary<string, ErrorDetails> errors = null);
4543
}
4644
}

src/LoggerExtensions.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,27 @@ internal static class LoggerExtensions
99
public static void Error(this ILogger logger, Exception exception, string message)
1010
=> logger.LogError(
1111
exception,
12-
$"[ERROR HANDLER] > {message}"
13-
);
12+
$"[ERROR HANDLER] > {message}");
1413

1514

1615
public static void Error(this ILogger logger, Exception exception, string request, int? statusCode)
1716
=> logger.LogError(
1817
exception,
19-
$"[ERROR HANDLER] > Request: '{request}', StatusCode: '{statusCode}'"
20-
);
18+
$"[ERROR HANDLER] > Request: '{request}', StatusCode: '{statusCode}'");
2119

2220
public static void Error(this ILogger logger, Exception exception, string request, int? statusCode, string message)
2321
=> logger.LogError(
2422
exception,
25-
$"[ERROR HANDLER] > Request: '{request}', StatusCode: '{statusCode}' > {message}"
26-
);
23+
$"[ERROR HANDLER] > Request: '{request}', StatusCode: '{statusCode}' > {message}");
2724

2825

2926

3027
public static void Debug(this ILogger logger, string message)
3128
=> logger.LogDebug(
32-
$"[ERROR HANDLER] > {message}"
33-
);
29+
$"[ERROR HANDLER] > {message}");
3430

3531
public static void Debug(this ILogger logger, HttpContext httpContext, string message)
3632
=> logger.LogDebug(
37-
$"[ERROR HANDLER] > Request: '{httpContext.GetRequestEndpoint()}', StatusCode: '{httpContext.GetStatusCode()}' > {message}"
38-
);
33+
$"[ERROR HANDLER] > Request: '{httpContext.GetRequestEndpoint()}', StatusCode: '{httpContext.GetStatusCode()}' > {message}");
3934
}
4035
}

src/ModelStateExtensions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ public static IDictionary<string, ErrorDetails> CheckPayloadTooLargeAndReturnErr
3939
.First()
4040
.ToLower();
4141

42-
if(
43-
errorMessage.StartsWith("failed to read the request form. multipart body length limit ")
44-
&&
45-
errorMessage.EndsWith(" exceeded.")
46-
)
42+
if(errorMessage.StartsWith("failed to read the request form. multipart body length limit ")
43+
&&
44+
errorMessage.EndsWith(" exceeded."))
4745
{
4846
var maxSize = errorMessage
4947
.Replace("failed to read the request form. multipart body length limit ", "")

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/Tests/Controllers/ExceptionsControllerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public async void EndpointWithUnauthorizedAccessException_Request_401()
8787

8888

8989
// Assert
90-
9190
using(new AssertionScope())
9291
{
9392
response.ValidateResponse(HttpStatusCode.Unauthorized);

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/Tests/Controllers/FilesControllerTests.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net;
55
using System.Net.Http;
66
using System.Threading.Tasks;
7+
using FluentAssertions.Execution;
78
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.Extensions.Options;
910
using PowerUtils.AspNetCore.ErrorHandler.Tests.Config;
@@ -35,23 +36,26 @@ public async Task FileLarge_Send_413()
3536

3637

3738
// Act
39+
await _testsFixture.Client.SendPostMultipartAsync(requestUri, body);
3840
(var response, var content) = await _testsFixture.Client.SendPostMultipartAsync(requestUri, body);
3941
options.Value.ClientErrorMapping.TryGetValue((int)response.StatusCode, out var clientErrorData);
4042

4143

4244
// Assert
43-
response.ValidateResponse(HttpStatusCode.RequestEntityTooLarge);
44-
45-
content.ValidateContent(
46-
HttpStatusCode.RequestEntityTooLarge,
47-
clientErrorData,
48-
"POST: " + requestUri,
49-
"The payload is too big.",
50-
new Dictionary<string, ErrorDetails>()
51-
{
52-
{ "payload", new("MAX:1048576", "The payload is too big.") }
53-
}
54-
);
45+
using(new AssertionScope())
46+
{
47+
response.ValidateResponse(HttpStatusCode.RequestEntityTooLarge);
48+
49+
content.ValidateContent(
50+
HttpStatusCode.RequestEntityTooLarge,
51+
clientErrorData,
52+
"POST: " + requestUri,
53+
"The payload is too big.",
54+
new Dictionary<string, ErrorDetails>()
55+
{
56+
{ "payload", new("MAX:1048576", "The payload is too big.") }
57+
});
58+
}
5559
}
5660
}
5761
}

0 commit comments

Comments
 (0)