title: Best practice to return errors in ASP.NET Web API
body: <p>I have concerns on the way that we returns errors to client.</p>
  
  <p>Do we return error immediately by throwing <a href="https://msdn.microsoft.com/en-us/library/system.web.http.httpresponseexception(v=vs.118).aspx" rel="noreferrer">HttpResponseException</a> when we get an error:</p>
  
  <pre><code>public void Post(Customer customer)
  {
      if (string.IsNullOrEmpty(customer.Name))
      {
          throw new HttpResponseException("Customer Name cannot be empty", HttpStatusCode.BadRequest) 
      }
      if (customer.Accounts.Count == 0)
      {
           throw new HttpResponseException("Customer does not have any account", HttpStatusCode.BadRequest) 
      }
  }
  </code></pre>
  
  <p>Or we accumulate all errors then send back to client:</p>
  
  <pre><code>public void Post(Customer customer)
  {
      List&lt;string&gt; errors = new List&lt;string&gt;();
      if (string.IsNullOrEmpty(customer.Name))
      {
          errors.Add("Customer Name cannot be empty"); 
      }
      if (customer.Accounts.Count == 0)
      {
           errors.Add("Customer does not have any account"); 
      }
      var responseMessage = new HttpResponseMessage&lt;List&lt;string&gt;&gt;(errors, HttpStatusCode.BadRequest);
      throw new HttpResponseException(responseMessage);
  }
  </code></pre>
  
  <p>This is just a sample code, it does not matter either validation errors or server error, I just would like to know the best practice, the pros and cons of each approach.</p>