title: How do I get the real error message in HttpClient response.ReasonPhrase?
body: <h2>My Setting</h2>
  
  <p>I have 2 WebApi projects with the following flow:</p>
  
  <ol>
  <li><strong>User</strong> makes request to <strong>API 1</strong></li>
  <li><strong>API 1</strong> makes request to <strong>API 2</strong> on behalf of the user (using an <code>HttpClient</code>).</li>
  </ol>
  
  <hr>
  
  <pre><code>using (var client = new HttpClient())
  {
      client.SetBearerToken(token);
  
      string endpoint = PbbSettings.Identity.Users.Delete.Replace("{userId}", userId);
  
      // Attempt deletion of the user
      using (var response = await client.DeleteAsync(endpoint))
      {
          // Throw exception if not succeeded
          EnsureSuccess(response);
      }
  }
  </code></pre>
  
  <h2>The Problem</h2>
  
  <p>So the flow of control and information works fine.
  The problem is that when <strong>API 2</strong> responds with an error, <code>response.ReasonPhrase</code> says "Bad Request" or "Internal Server Error", instead of the message I set in the exception.</p>
  
  <p>Been spitting blood on this for a whole day now. Any insights?</p>
  
  <h2>More Info (TLDR)</h2>
  
  <p>For clarity, all my APIs have a global exception filter registered to handle errors:</p>
  
  <pre><code>public class RepositoryExceptionsHandlerAttribute : ExceptionFilterAttribute
  {
      public override void OnException(HttpActionExecutedContext context)
      {
          HandleException(context);
  
          base.OnException(context);
      }
  
      public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
      {
          HandleException(context);
  
          return base.OnExceptionAsync(context, cancellationToken);
      }
  
      /// &lt;summary&gt;
      /// Recognizes common repository exceptions and if necessary creates a response and updates the context.
      /// &lt;/summary&gt;
      /// &lt;param name="context"&gt;The context in which the exception was thrown.&lt;/param&gt;
      private void HandleException(HttpActionExecutedContext context)
      {
          var response = CreateResponse(context.Request, context.Exception);
  
          if (response != null)
              context.Response = response;
      }
  
      /// &lt;summary&gt;
      /// Recognizes common repository exceptions and creates a corresponding error response.
      /// &lt;/summary&gt;
      /// &lt;param name="request"&gt;The request to which the response should be created.&lt;/param&gt;
      /// &lt;param name="ex"&gt;The exception to handle.&lt;/param&gt;
      /// &lt;returns&gt;An error response containing the status code and exception data, or null if this is not a common exception.&lt;/returns&gt;
      private HttpResponseMessage CreateResponse(HttpRequestMessage request, Exception ex)
      {
          string message = ex.Message;
  
          if (ex is KeyNotFoundException)        return request.CreateErrorResponse(HttpStatusCode.NotFound, message);
          if (ex is ArgumentException)           return request.CreateErrorResponse(HttpStatusCode.BadRequest, message);
          if (ex is InvalidOperationException)   return request.CreateErrorResponse(HttpStatusCode.BadRequest, message);
          if (ex is UnauthorizedAccessException) return request.CreateErrorResponse(HttpStatusCode.Unauthorized, message);
  #if !DEBUG
          // For security reasons, when an exception is not handled the system should return a general error, not exposing the real error information
          // In development time, the programmer will need the details of the error, so this general message is disabled.
          request.CreateErrorResponse(HttpStatusCode.InternalServerError, Errors.InternalServerError);
  #endif
  
          return null;
      }
  }
  </code></pre>
  
  <p>This works fine between the <strong>user</strong> and <strong>API 1</strong>.
  But when <strong>API 1</strong> and <strong>API 2</strong> do their thing, the response creation ignores the message I put in and sets the status as the reason.</p>