title: How to get the exception from HttpResponseMessage in web API?
body: <p>I'm trying to implement a custom error handling in web API,
  and I need to get the exception from the returned HttpResponseMessage.</p>
  
  <p>I've tried to get exception info by from:</p>
  
  <pre><code>response.Content.ReadAsAsync&lt;HttpError&gt;().Result
  </code></pre>
  
  <p>But I can't access the result object, I'm getting an exception when trying,
  So I'm obviously doing it wrong.</p>
  
  <p>Can't figure out how to do it,
  Assistance would be appreciated.</p>
  
  <p>Edit:</p>
  
  <p>My client code is not relevant, it's simply a GET request, server code:</p>
  
  <p>Controller action throws Exception:</p>
  
  <pre><code>if (condition == true)
  {
      var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
      {
          Content = new StringContent("Some Exception Related Message"),
          ReasonPhrase = "Some More Info"
      };
  
      throw new HttpResponseException(response);
  }
  </code></pre>
  
  <p>SendAsync method of my implemented DelegatingHandler gets the response,
  and this is where I want to get the callstack of the exception that was thrown in the controller action above.</p>
  
  <pre><code>errorDetails = new ResponseErrorDetailsFull
  {
      Message = "An error has occurred.",
      ExceptionMessage = response.ReasonPhrase,
      StackTrace = response.Content.ReadAsAsync&lt;HttpError&gt;().Result.StackTrace 
  };
  </code></pre>
  
  <p>Edit #2</p>
  
  <p>Ok, So I found out that if I create a ExceptionFilterAttribute, and Override OnException(), use the attribute on my DelegatingHandler I'm able to access the exception as mentioned in the above code.</p>
  
  <p>Can someone provide an explanation why this is working this way?</p>