title: How to idiomatically handle HTTP error codes when using RestSharp?
body: <p>I'm building an HTTP API client using RestSharp, and I've noticed that when the server returns an HTTP error code (401 Unauthorized, 404 Not Found, 500 Internal Server Error, etc.) the <code>RestClient.Execute()</code> doesn't throw an exception - instead I get a valid <code>RestResponse</code> with a null <code>.Data</code> property. I don't want to manually check for every possible HTTP error code within my API client - does RestSharp provide a better way of passing these errors to my client application?</p>
  
  <p>A little further detail. RestSharp exposes a <code>Response.ErrorException</code> property - if the <code>RestClient.Execute&lt;T&gt;()</code> call causes any exception, it'll be exposed via the <code>ErrorException</code> property instead of being thrown. Their documentation includes the following example:</p>
  
  <pre><code>// TwilioApi.cs
  public class TwilioApi {
      const string BaseUrl = "https://api.twilio.com/2008-08-01";
  
      public T Execute&lt;T&gt;(RestRequest request) where T : new()
      {
      var client = new RestClient();
      client.BaseUrl = BaseUrl;
      client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
      request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment); // used on every request
      var response = client.Execute&lt;T&gt;(request);
  
      if (response.ErrorException != null)
      {
          const string message = "Error retrieving response.  Check inner details for more info.";
          var twilioException = new ApplicationException(message, response.ErrorException);
          throw twilioException;
      }
      return response.Data;
      }
  
  }
  </code></pre>
  
  <p>I've adopted that pattern in my code, but my API server is returning a <code>401 Unauthorized</code> and yet the ErrorException property is still null. I <em>can</em> see the <img src="https://i.stack.imgur.com/Drg2k.png" alt="RestResponse debug watch"> Unauthorized status code and error message in the <code>RestResponse.StatusCode</code> and <code>RestResponse.StatusDescription</code> properties - but I'm confused as to why an unauthorized response wouldn't result in the <code>ErrorException</code> field being populated.</p>