title: dealing with an error from a typed WebAPI HttpClient call
body: <p>I have a number of calls I make to a webapi client which return a Task 
  something like this</p>
  
  <pre><code>public async Task&lt;TResp&gt; GetMyThingAsync(TReq req)
  {
      using (HttpClient client = new HttpClient())
      {
          client.BaseAddress = new Uri(BaseURI);
          client.DefaultRequestHeaders.Accept.Clear();
          client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
          client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
  
          await HttpRuntime.Cache.GetToken().ContinueWith((t) =&gt;
          {
              client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("XXX", t.Result);
          });
  
          var httpResponseMessage = await client.PostAsXmlAsync&lt;TReq&gt;("This/That/", req);
          httpResponseMessage.EnsureSuccessStatusCode();
          var resp = await httpResponseMessage.Content.ReadAsAsync&lt;TResp&gt;();
          return resp;
      }
  }
  </code></pre>
  
  <p>the calls to the api can of course return 500's or some other problem. <code>EnsureSuccessStatusCode()</code> obviously throws if something like that happens, but then its too late to do anything with any information in the response. 
  is there a nice way of dealing with this?
  I understand you can add a messageHandler with the client, something like
  <code>HttpClient client = HttpClientFactory(new ErrorMessageHandler())</code> ..</p>
  
  <pre><code>var customHandler = new ErrorMessageHandler()
          { InnerHandler = new HttpClientHandler()};
  HttpClient client = new HttpClient(customHandler);
  </code></pre>
  
  <p>is this the way to go? what would the ErroMessageHandler look like and do to return something useful to the calling controller...?</p>
  
  <p>thanks muchly</p>
  
  <p>nat</p>