title: Throw HttpResponseException or return Request.CreateErrorResponse?
body: <p>After reviewing an article <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling" rel="noreferrer">Exception Handling in ASP.NET Web API</a> I am a bit confused as to when to throw an exception vs return an error response. I am also left wondering whether it is possible to modify the response when your method returns a domain specific model instead of <code>HttpResponseMessage</code>...</p>
  
  <p>So, to recap here are my questions followed by some code with case #s:</p>
  
  <h1>Questions</h1>
  
  <h2>Questions regarding Case #1</h2>
  
  <ol>
  <li>Should I always use <code>HttpResponseMessage</code> instead of a concrete domain model, so that the message can be customized?</li>
  <li>Can the message be customized if you are returning concrete domain model?     </li>
  </ol>
  
  <h2>Questions regarding Case #2,3,4</h2>
  
  <ol start="3">
  <li>Should I be throwing an exception or returning error response? If the answer is "it depends", can you give situations/examples on when to use one vs the other. </li>
  <li>What is the difference between throwing <code>HttpResponseException</code> vs <code>Request.CreateErrorResponse</code> ? The output to client seems identical...</li>
  <li>Should I always use <code>HttpError</code> to "wrap" response messages in errors (whether the exception is thrown or error response returned)?</li>
  </ol>
  
  <h1>Case Samples</h1>
  
  <pre><code>// CASE #1
  public Customer Get(string id)
  {
      var customer = _customerService.GetById(id);
      if (customer == null)
      {
          var notFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound);
          throw new HttpResponseException(notFoundResponse);
      }
      //var response = Request.CreateResponse(HttpStatusCode.OK, customer);
      //response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
      return customer;
  }        
  
  // CASE #2
  public HttpResponseMessage Get(string id)
  {
      var customer = _customerService.GetById(id);
      if (customer == null)
      {
          var notFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound);
          throw new HttpResponseException(notFoundResponse);
      }
      var response = Request.CreateResponse(HttpStatusCode.OK, customer);
      response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
      return response;
  }
  
  // CASE #3
  public HttpResponseMessage Get(string id)
  {
      var customer = _customerService.GetById(id);
      if (customer == null)
      {
          var message = String.Format("customer with id: {0} was not found", id);
          var errorResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
          throw new HttpResponseException(errorResponse);
      }
      var response = Request.CreateResponse(HttpStatusCode.OK, customer);
      response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
      return response;
  }
  
  // CASE #4
  public HttpResponseMessage Get(string id)
  {
      var customer = _customerService.GetById(id);
      if (customer == null)
      {
          var message = String.Format("customer with id: {0} was not found", id);
          var httpError = new HttpError(message);
          return Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
      }
      var response = Request.CreateResponse(HttpStatusCode.OK, customer);
      response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
      return response;
  }
  </code></pre>
  
  <h1>Update</h1>
  
  <p>To help further demonstrate cases #2,3,4 the following code snippet highlights several options that "can happen" when a customer is not found...</p>
  
  <pre><code>if (customer == null)
  {
      // which of these 4 options is the best strategy for Web API?
  
      // option 1 (throw)
      var notFoundMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
      throw new HttpResponseException(notFoundMessage);
  
      // option 2 (throw w/ HttpError)
      var message = String.Format("Customer with id: {0} was not found", id);
      var httpError = new HttpError(message);
      var errorResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
      throw new HttpResponseException(errorResponse);
  
      // option 3 (return)
      var message = String.Format("Customer with id: {0} was not found", id);
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
      // option 4 (return w/ HttpError)
      var message = String.Format("Customer with id: {0} was not found", id);
      var httpError = new HttpError(message);
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, httpError);
  }
  </code></pre>