title: Adding more detail to await Task<T> in C# .net
body: <p>The Task object created in C# contains the details for the completion of the Action ascribed to the Task. So even if the Action fails the task is still IsCompleted, unless the Task throws an exception or is cancelled.</p>
  <p>What I am trying to do is to create a Task-Like class (easy, <a href="https://gist.github.com/Horusiath/401ed16563dd442980de681d384f25b9" rel="nofollow noreferrer">done that bit</a>) that can store more information about the job that was done.</p>
  <p>So for example I want an API to get some accounts, the results may throw an exception, say SQLConnection Exception for example, fine, but also the results may be empty and I want to know why. Is the Database empty or has a filter restricted the view to nothing, for example?</p>
  <p>So I could do:</p>
  <pre><code>var t = AccountResposistory.GetAccountsAsync(filter, token);
  var results = await t;
  if(t.Success)
  {
      return Results.Ok(results)
  }
  else
  {
      return Results.Problem(t.ErrorMessage);
  }
  </code></pre>
  <p>I was heading down the path of using a custom Task-Like object something like:</p>
  <pre><code>public async ServiceTask&lt;IEnumerable&lt;Account&gt;?&gt; GetAccountsAsync(Filter filter, CancellationToken token)
  {
      // Get accounts with filtering
      if(Accounts.Any())
      {
          return Accounts;
      }
      else if(filter != null)
      {
          return ServiceTask.FromError(&quot;Filter has restricted all Accounts&quot;);
      }
      else
      {
          return ServiceTask.FromError(&quot;Database doesn't contain any Accounts&quot;);
      }
  }
  </code></pre>
  <p>I can have ServiceTask.FromError return a default T but there's no way (I can see) to access the ServiceTask that's returned by the method to add in the details.</p>
  <p>Alternatively, I figured I could have ServiceTask always return a generic Response class and then work with the properties inside ServiceTask to apply the results or messages, but I can't figure out how to do that, how to restrict ServiceTask that T is always a generic class of ServiceResponse</p>
  <p>I don't like the idea of throwing exceptions when an exception hasn't happened. It's not a code exception when the database is empty, or the filter has removed all accounts.</p>
  <p>Currently, my GetAccountsAsync is returning a Task&lt;Response&gt; and the Response has Success, ErrorMessage and Results properties. It becomes a bit cumbersome to work around this, to get the results of the awaited results. I'm hoping there's a simple way to code this.</p>