-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add support to multiple HTTP response status codes π§βπ» #16
Open
luizhlelis
wants to merge
6
commits into
juntossomosmais:main
Choose a base branch
from
luizhlelis:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83175dd
chore: adding error response classes β¬οΈ
luizhlelis 586e707
chore: multiple error code responses logic βοΈ
luizhlelis a5638ae
chore: multiple error code responses coverage βοΈ
luizhlelis 8c2d53e
fix: classes can be internal π
luizhlelis 42a2aae
docs: summary and documentation π
luizhlelis 2de0412
chore: use `ProblemDetails`, support to unauthorized β
luizhlelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace JSM.FluentValidation.AspNet.AsyncFilter.ErrorResponse | ||
{ | ||
/// <summary> | ||
/// Defines what HTTP status code should be returned. Use it with the `WithErrorCode` extension method. | ||
/// </summary> | ||
public static class ErrorCode | ||
{ | ||
/// <summary> | ||
/// 401 HTTP status code as per RFC 2616 | ||
/// </summary> | ||
public const string Unauthorized = "UNAUTHORIZED_ERROR"; | ||
|
||
/// <summary> | ||
/// 403 HTTP status code as per RFC 2616 | ||
/// </summary> | ||
public const string Forbidden = "FORBIDDEN_ERROR"; | ||
|
||
/// <summary> | ||
/// 404 HTTP status code as per RFC 2616 | ||
/// </summary> | ||
public const string NotFound = "NOT_FOUND_ERROR"; | ||
|
||
internal static readonly HashSet<string> AvailableCodes = new HashSet<string>() | ||
{Unauthorized, Forbidden, NotFound}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace JSM.FluentValidation.AspNet.AsyncFilter.ErrorResponse | ||
{ | ||
internal static class ErrorResponseFactory | ||
{ | ||
public static TraceableProblemDetails CreateErrorResponse(ModelStateDictionary modelState, | ||
string traceparent) | ||
{ | ||
if (modelState[ErrorCode.Unauthorized] is not null) | ||
return new UnauthorizedResponse( | ||
modelState[ErrorCode.Unauthorized]?.Errors.FirstOrDefault()?.ErrorMessage ?? | ||
string.Empty, traceparent); | ||
|
||
if (modelState[ErrorCode.Forbidden] is not null) | ||
return new ForbiddenResponse( | ||
modelState[ErrorCode.Forbidden]?.Errors.FirstOrDefault()?.ErrorMessage ?? | ||
string.Empty, traceparent); | ||
|
||
return new NotFoundResponse( | ||
modelState[ErrorCode.NotFound]?.Errors.FirstOrDefault()?.ErrorMessage ?? | ||
string.Empty, | ||
traceparent); | ||
} | ||
|
||
public static HttpStatusCode GetResponseStatusCode(ModelStateDictionary modelState) | ||
{ | ||
if (modelState[ErrorCode.Unauthorized] is not null) | ||
return HttpStatusCode.Unauthorized; | ||
|
||
if (modelState[ErrorCode.Forbidden] is not null) | ||
return HttpStatusCode.Forbidden; | ||
|
||
return modelState[ErrorCode.NotFound] is not null | ||
? HttpStatusCode.NotFound | ||
: HttpStatusCode.BadRequest; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace JSM.FluentValidation.AspNet.AsyncFilter.ErrorResponse | ||
{ | ||
internal class ForbiddenResponse : TraceableProblemDetails | ||
{ | ||
public ForbiddenResponse(string message, string traceparent) | ||
{ | ||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.3"; | ||
Title = ErrorCode.Forbidden; | ||
Status = 403; | ||
Detail = message; | ||
TraceId = traceparent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Text.Json.Serialization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace JSM.FluentValidation.AspNet.AsyncFilter.ErrorResponse | ||
{ | ||
internal class NotFoundResponse : TraceableProblemDetails | ||
{ | ||
public NotFoundResponse(string message, string traceparent) | ||
{ | ||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.4"; | ||
Title = ErrorCode.NotFound; | ||
Status = 404; | ||
Detail = message; | ||
TraceId = traceparent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Text.Json.Serialization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace JSM.FluentValidation.AspNet.AsyncFilter.ErrorResponse | ||
{ | ||
internal abstract class TraceableProblemDetails : ProblemDetails | ||
{ | ||
/// <summary> | ||
/// A unique identifier responsible to describe the incoming request. | ||
/// </summary> | ||
[JsonPropertyName("traceId")] | ||
public string TraceId { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace JSM.FluentValidation.AspNet.AsyncFilter.ErrorResponse | ||
{ | ||
internal class UnauthorizedResponse : TraceableProblemDetails | ||
{ | ||
public UnauthorizedResponse(string message, string traceparent) | ||
{ | ||
Type = "https://datatracker.ietf.org/doc/html/rfc7235#section-3.1"; | ||
Title = ErrorCode.Unauthorized; | ||
Status = 401; | ||
Detail = message; | ||
TraceId = traceparent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
ο»Ώusing FluentAssertions; | ||
using JSM.FluentValidation.AspNet.AsyncFilter.Tests.Support; | ||
using JSM.FluentValidation.AspNet.AsyncFilter.Tests.Support.Extensions; | ||
using JSM.FluentValidation.AspNet.AsyncFilter.Tests.Support.Models; | ||
using JSM.FluentValidation.AspNet.AsyncFilter.Tests.Support.Startups; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using System.Net.Http.Json; | ||
|
||
namespace JSM.FluentValidation.AspNet.AsyncFilter.Tests | ||
{ | ||
|
@@ -25,7 +25,7 @@ public async Task OnActionExecutionAsync_ControllerDoesNotHaveApiControllerAttri | |
|
||
// Act | ||
var response = | ||
await Client.PostAsJsonAsync($"{ControllerWithoutApiAttributeEndpoint}/test-validator", payload); | ||
await HttpClientJsonExtensions.PostAsJsonAsync(Client, $"{ControllerWithoutApiAttributeEndpoint}/test-validator", payload); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: It's realy a minor issue, but I didn't understand why changing the call to |
||
|
||
// Assert | ||
response.Should().Be200Ok(); | ||
|
@@ -39,10 +39,10 @@ public async Task OnActionExecutionAsync_ControllerHasApiControllerAttribute_Ret | |
|
||
// Act | ||
var response = | ||
await Client.PostAsJsonAsync($"{ControllerWithApiAttributeEndpoint}/test-validator", payload); | ||
await HttpClientJsonExtensions.PostAsJsonAsync(Client, $"{ControllerWithApiAttributeEndpoint}/test-validator", payload); | ||
|
||
// Assert | ||
response.Should().Be400BadRequest(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: The order of the error entries affects the result? If the first validation that fails is a "normal" one and the second one has an
.WithErrorCode(ErrorCode.Forbidden);
what would be the the error code?If so, maybe we should add this info to the README