Skip to content
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

[Question] How to handle unsuccesful results at API level #171

Closed
dkamau opened this issue Mar 25, 2024 · 6 comments
Closed

[Question] How to handle unsuccesful results at API level #171

dkamau opened this issue Mar 25, 2024 · 6 comments

Comments

@dkamau
Copy link

dkamau commented Mar 25, 2024

For example, if I sent back a conflict from my handler;

return Result.Conflict($"Contributor with name {request.NewName} already exists!");

How will I report that to the user at the API level?

public override async Task HandleAsync(
  CreateContributorRequest request,
  CancellationToken cancellationToken)
{
  var result = await _mediator.Send(new CreateContributorCommand(request.Name!,
    request.PhoneNumber), cancellationToken);

  if (result.IsSuccess)
  {
    Response = new CreateContributorResponse(result.Value, request.Name!);
    return;
  }
  // TODO: Handle other cases as necessary

  if(result.Status == Ardalis.Result.ResultStatus.Conflict)
  {
    Response = ? "What code goes here??";
    return;
  }
}

I tried this but it does not report as expected

if(result.Status == Ardalis.Result.ResultStatus.Conflict)
{
  Response = Result<CreateContributorResponse>.Conflict(result.Errors.ToString());
  return;
}
@KyleMcMaster
Copy link
Collaborator

@dkamau Are you using FastEndpoints?

@dkamau
Copy link
Author

dkamau commented Mar 28, 2024

@KyleMcMaster Yes I am.

@KyleMcMaster
Copy link
Collaborator

We've discussed adding FastEndpoints support in issue #157

@sunecko
Copy link
Contributor

sunecko commented May 4, 2024

i using fast endpoit around six months on my work , i think it's a great idea add FE support to Results. I wanna participate

@KyleMcMaster
Copy link
Collaborator

There is work being done to provide support for FastEndpoints similar to the functionality for ASP.NET Core support. Consider 👍 'ing the issue in #175 if you'd like to see this implemented.

@ardalis
Copy link
Owner

ardalis commented May 16, 2024

See this comment from #175:

FastEndpoint already has a SendResultAsync() to send any IResult instance produced by the Results static class in Minimal APIs. So simply you can call something like this:
await SendResultAsync(result.ToMinimalApiResult());
More info: https://fast-endpoints.com/docs/misc-conveniences#send-methods

I'm assuming that either this fixes the immediate issue or, if not, that #175 will do so, so I'm going to close this now. Re-open if you still need to.

@ardalis ardalis closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants