amantinband/error-or

[Enhancement] Add extension methods for to convert to IActionResult (mvc) IResult (minimal API)

RHaughton opened this issue · 22 comments

First of all, I love this extension.
I would like to see extension methods to convert an ErrorOr (domain result) to an IActionResult or IResult.

The conversion should send 200OK or 204NoContent if the operation is successfull.
If the result is an Error the conversion would take the type of error and return an appropriate ProblemDetails with the correct status Code.

This enhancement was inspired by this repository https://github.com/AKlaus/DomainResult

Maybe as a workaround (if this won't get implemented) you could use the really convenient Match method.

e.g

 private async Task<IResult> GetBookById(long id, IBookRepo bookRepo)
    {
        var book = await bookRepo.GetBookByIdAsync(id);

        return book.Match(
            success => Results.Ok(success),
            failure => Results.Problem()
        );
    }

where the IBookRepo returns ErrorOr<Book>. Just a quick demo, you should process the list of errors (failure) and return matching results. 500 UnexpectedError, 400 BadRequest, ...