0

I have the following method:

public Task<Envelope<Response>> Handle(Request request, CancellationToken cancellationToken) {

  List<Error> errors = _validator.Validate(request));

  if (errors.Any()) 
    return new Envelope<Response>(errors);

  return _handler.Handle(request, cancellationToken);

}

If there are any errors I need to to return Envelope<Response>(errors).

The problem is the method returns Task<Envelope<Response>>.

How to create and return the Task?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • How common are errors? Maybe you should consider using [Exceptions](https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions) instead. – Hyarus Aug 29 '18 at 11:21
  • I do not want to use Exceptions as these are Model Validation errors. I was able to solve it with Task.FromResult: return Task.FromResult>(new Envelope(errors)); – Miguel Moura Aug 29 '18 at 11:30

0 Answers0