title: Correct use of EnsureSuccessStatusCode and IsSuccessStatusCode
body: <p>I'm calling my Web API using an <code>HttpClient</code> and I see that there is an <code>EnsureSuccessStatusCode</code> method and an <code>IsSuccessStatusCode</code> property. Which one is appropriate? </p>
  
  <p>I read this article and I have a few additional questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/21097730/usage-of-ensuresuccessstatuscode-and-handling-of-httprequestexception-it-throws">Usage of EnsureSuccessStatusCode and handling of HttpRequestException it throws</a></p>
  
  <p>The issue I'm having is that if I send a GET request where I pass the <code>ID</code> of the object I want to retrieve, there are basically two outcomes: </p>
  
  <ol>
  <li>I get the object back with a status 200</li>
  <li>I could get null back because there was no match, however this results in a status of 404.</li>
  </ol>
  
  <p>If I call <code>EnsureSuccessStatusCode()</code> a status 404 would result in an exception being thrown. That isn't ideal because when I was testing my code I kept getting a 404 error which at first made me think that the API URL was incorrect, but in reality there was no matching object with the supplied Id. In this case, I'd rather return a null object instead of throwing an exception.</p>
  
  <p>So instead I tried checking the value of the <code>IsSuccessfulStatusCode</code> property. That seemed like a better choice and I can return a null object when this property is false, however, there are many status codes that can result in this property having a false value. 404 is one of them, but there are several other status codes, such as <strong>400 Bad Request</strong>, <strong>405 Method Not Allowed</strong>, etc. I'd like to log an exception for all unsuccessful error codes except for the 404's and I'm wondering if there is a better way to do this than inspecting the <code>ResponseCode</code> value of the response and then throwing an exception which gets caught by my catch block, which is where the logging takes place.</p>
  
  <p>Here's the code for my GET method:</p>
  
  <pre><code>public static Customer GetCustomerByID(int id)
  {
      try
          {
              using (var client = GetConfiguredClient())
              {
                  Customer customer = null;
                  var requestUri = $"Customers/{id}";
  
                  using (var response = client.GetAsync(requestUri).Result)
                  {
                      if (response.IsSuccessStatusCode)
                          customer = response.Content.ReadAsAsync&lt;Customer&gt;().Result;
                  }
  
                  return customer;
              }
          }
          catch (Exception ex)
          {
            ex.Data.Add(nameof(id), id);
  
            LogException(ex);
  
            throw;
          }
      }
  </code></pre>
  
  <p>This code will just return a null <code>Customer</code> if a non-successful status code is returned and nothing gets logged.</p>
  
  <p>What is the best way to handle this situation?</p>