Minimal API: TypedResults, Problem, ValidationProblem #59560
Labels
area-minimal
Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
design-proposal
This issue represents a design proposal for a different issue, linked in the description
Summary
Enhance the way we return
TypedResult
for minimal api andProblemDetails
when the system raise an error.Motivation and goals
I believe the newly added features related to this topic are great, but there is room for improvement.
In scope
Out of scope
N/A
Risks / unknowns
None
Examples
The way we can now use the following code is great:
Enriching our problem details in this manner is simply ❤
For exception handling in the case of a real exception, the ability to inject the
IProblemDetailsService
service is cool:However, I believe there is a need for more clarity regarding when we are serious about error handling and do not want to raise an exception in the case of a normal error. An exception should indicate an unplanned error.
Now that we have the TypedResults extension and a way to enforce our Minimal API to return certain types, ValidationProblem seems unnecessary because ultimately, we want to return either our normal typed result or a typed ProblemDetails result.
Something like that:
Task<Results<Created<TenantStandardResult>, ProblemHttpResult >>
Not something like that
Task<Results<Created<TenantStandardResult>, ProblemHttpResult, ValidationProblem >>
On my end, to always be able to return
ProblemDetails
in the case of "business" errors, validation errors, or exceptions, I created a new extension calledCustomTypedResults
as follows:With that, I m able to pass the result of a fluent validation (Dic) (like before with TypedResults.ValidationProblem():
CustomTypedResults.Problem()
Or pass my error interface (IFeatureError), the same way:
CustomTypedResults.Problem()
And maintain my typed API returns simple:
private async Task<Results<Created<TenantStandardResult>, ProblemHttpResult >>
Detailed design
I am unsure if this approach will be interesting for others, but I believe it simplifies the process and allows us to consistently return
ProblemDetails
without further consideration.Mainly when you don't use exception handler to trap normal errors...
I think it's not good and I prefer very much the
Either<IFeatureError,RightResult>
pattern and be able to transform aIFeatureError
toProblemDetails
.I am curious if it would be beneficial to explore this topic further or if anyone has a better approach already.
The text was updated successfully, but these errors were encountered: