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
Mapping Exception x StatusCode in ExceptionHandlerMiddleware #44156
Comments
I like the dictionary because it gives the ability to manipulate the data (CRUD). IMO |
I like it as well :) and using a |
However, it feels very limiting to restrict mapping from exceptions to status codes. Maybe this is just a first step? In my middleware, status code mapping is just a few higher level convenience methods that boil down to What's the rationale behind this limitation? Just keeping it simple? |
@khellang Thanks for the feedback. Do you have any usage telemetry of the convenience methods x underlying func lists? The
|
Thanks for contacting us. We're moving this issue to the |
Having the option to create typed ProblemDetails is quite useful in OpenApi where you can clearly define a possible list of "Problems" and additional information as per rfc7807 standard, without having to overwrite default ExceptionHandler behavior. One existing example would be to generate Microsoft.AspNetCore.Http.HttpValidationProblemDetails and include an Errors dictionary when there is a FluentValidation exception. So yes, having Func<HttpContext, Exception, ProblemDetails> would be ideal :) |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review notes:
API Approved! namespace Microsoft.AspNetCore.Builder;
public class ExceptionHandlerOptions
{
+ public Func<Exception, int>? StatusCodeSelector { get; set; }
} |
If nobody is planning to this yet. Can I work on it? |
I don't anyone is working on it yet @brunolins16 - it's all yours :) |
Thanks. I will have a PR later this week. |
What's the status on this? Is there a chance this will be released in the next few weeks? |
If the work to do this change hasn't already been merged into the release/8.0 branch (which I would assume not if this issue is still open with no updates since April), then it's too late for .NET 8. |
Background and Motivation
In .NET 7 the
ExceptionHandlerMiddleware
was updated to generate aProblem Details
when theIProblemDetailsService
is registered.While this change allows a consistent
ProblemDetails
for the application the default handler forExceptionHandlerMiddleware
always sets the response status code to500
and changing to produce a different Status Code requires a custom implementation (as mentioned here #43831).Proposed API
The community
ProblemDetails
middleware (https://github.com/khellang/Middleware) has a capability to map an Exception to Status Code when producing the payload. Since theExceptionHandlerMiddleware
is the built-in feature to handle exception, my suggestion is to add a similar feature to allow mappingexception type
Xstatus code
every time an exception is handled.Usage Examples
Configuring the
ExceptionHandlerOptions
Setting
ExceptionHandlerOptions
directly to the middlewareAlternative Designs
Alternative names
public void AddMap<TException>(int statusCode) where TException : Exception {}
public void MapStatusCode<TException>(int statusCode) where TException : Exception {}
public void MapToStatusCode<TException>(int statusCode) where TException : Exception {}
Using a property instead
namespace Microsoft.AspNetCore.Builder; public class ExceptionHandlerOptions { + public IDictionary<Type, int> StatusCodeMapping { get; } = new Dictionary<Type, int>(); }
The disadvantage of this design is the
Dictionary
potentially accepts any type, not necessarily anException
type.cc @khellang
The text was updated successfully, but these errors were encountered: