amantinband/error-or

MatchAsync giving an error when IActionResult is returned from Api Contoller.

Closed this issue · 3 comments

Hello guys,

I am using ErrorOr (https://github.com/amantinband/error-or) library to return responses from my Api. However when I return response from my api, it is giving this error:

The type arguments for method 'ErrorOr<GenerateToken.Response>.MatchAsync(Func<GenerateToken.Response, Task>, Func<List, Task>)' cannot be inferred from the usage. Try specifying the type arguments

The error is on MatchAsync method call. Following is the code:

[HttpPost("token", Name = "GenerateToken")]
[ProducesResponseType<GenerateToken.Response>((int)HttpStatusCode.OK)]
public async Task<IActionResult> GenerateToken(GenerateToken.Command command)
{
    var response = await mediator.Send(command);

    return await response.MatchAsync(
        value => Ok(value),
        errors => BadRequest(errors));
}

What I am doing wrong here? Any help will be appreciated.

The get around I got is

return await response.MatchAsync(value => Task.FromResult<IActionResult>(Ok(value)), errors => Task.FromResult<IActionResult>(BadRequest(errors)));

which is not clean I guess because I have to mention "Task.FromResult" everywhere. Any work around for that?

 return response.Match<IActionResult>(
        value => Ok(value),
        errors => BadRequest(errors));

should work. The error is caused by using MathAsync with non async callbacks. That's why wrapping the callbacks with Task.FromResult make it work.

Yeah, got it now. Thank you.