-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add an option on the ExceptionHandlerOptions
to select status codes based on exceptions
#56616
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
Add an option on the ExceptionHandlerOptions
to select status codes based on exceptions
#56616
Conversation
…tionHandlerOptions
@dotnet-policy-service agree |
@latonz Thanks for the contribution! We'll review this soon. |
} | ||
|
||
[Fact] | ||
public async Task ExceptionHandler_SelectsStatusCode404_When404ThrowedAndNotNotAllowed() |
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.
This test name should be changed for clarity (and for the typo 😄)... maybe just StatusCodeSelector_CanSelect404
?
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.
Renamed the test as you suggested 👍
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs
Show resolved
Hide resolved
… a functional test
@@ -34,4 +35,21 @@ public async Task ExceptionHandlerPage_ProducesProblemDetails() | |||
Assert.NotNull(body); | |||
Assert.Equal(500, body.Status); | |||
} | |||
|
|||
[Fact] | |||
public async Task StatusCodeSelector_ProducesProblemDetailsWith403() |
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.
public async Task StatusCodeSelector_ProducesProblemDetailsWith403() | |
public async Task StatusCodeSelector_ProducesProblemDetailsWithCustomStatusCode() |
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.
Renamed the test as requested.
@@ -17,12 +17,22 @@ public void ConfigureServices(IServiceCollection services) | |||
public void Configure(IApplicationBuilder app) | |||
{ | |||
// Configure the error handler to produces a ProblemDetails. | |||
app.UseExceptionHandler(); | |||
app.UseExceptionHandler(new ExceptionHandlerOptions |
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.
Technically this makes the other test in ExceptionHandlderTest.cs not test the default behavior. Could we change it to something like
app.Map("/throw", throwApp =>
{
throwApp.UseExceptionHandler();
throwApp.Map("/conflict", throwConflictApp =>
{
throwConflictApp.Run(_ => throw new Exception("Conflict Exception"));
});
throwApp.Run(_ => throw new Exception("Application Exception"));
});
app.Map("/throw2", throwApp =>
{
throwApp.UseExceptionHandler(new ExceptionHandlerOptions
{
StatusCodeSelector = ex => ex is ConflictException
? StatusCodes.Status409Conflict
: StatusCodes.Status500InternalServerError,
});
throwApp.Map("/conflict", throwConflictApp =>
{
throwConflictApp.Run(_ => throw new Exception("Conflict Exception"));
});
});
So the tests can use different usages of UseExceptionHandler
?
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.
Thanks for pointing out, improved the isolation of the test as suggested.
7f94943
to
247a948
Compare
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
Thanks @latonz ! |
Add an option on the
ExceptionHandlerOptions
to select status codes based on exceptionsAdds
Func<Exception, int>? StatusCodeSelector
to theExceptionHandlerOptions
which allows users to easily map an exception to a status code. TheExceptionHandlerMiddlewareImpl
uses that new status code selector whenever the default status code of 500 was used and only falls back to 500 if no status code selector is set on the options.If a status code selector is set, 404 status codes are allowed (
AllowStatusCode404Response
is ignored).Fixes #44156