title: How do you return status 401 from WebAPI to AngularJS and also include a custom message?
body: <p>In my WebAPI class, an <code>ApiController</code>, I make a call such as:</p>
  
  <pre><code>string myCustomMessage = .....;
  
  throw new HttpResponseException(
      new HttpResponseMessage(HttpStatusCode.Unauthorized)
          { ReasonPhrase = myCustomMessage });
  </code></pre>
  
  <p>When I call using AngularJS <code>$resource</code> service, I do get 401 in the status field the response, in the catch block of the promise.   The 401 matches <code>HttpStatusCode.Unauthorized</code>, so all's well.</p>
  
  <p>The problem, however, is that the data field of the response is empty (null).   I don't get the myCustomMessage returned.</p>
  
  <p>Now, if rather than throwing a <code>HttpResponseException</code> exception, I just throw a regular <code>Exception</code> with a message, that message does make it sway back to Angular.   </p>
  
  <p>I need to be able to do both: return a custom message from the server, as well as have the returned status code be whatever I want, in this case 401.</p>
  
  <p>Anyone know how to make that work?</p>
  
  <p>[edit]
  <strong>Solution:</strong></p>
  
  <pre><code> throw new HttpResponseException(
       Request.CreateErrorResponse(HttpStatusCode.Unauthorized, myCustomMessage));
  </code></pre>