This is probably an AngularJS feature that you're already using; but, you may not be cognizant of the fact that it's happening. When you make an HTTP request with AngularJS, the request and response goes through a promise chain that applies a series of pre and post transformations. The default transformation, that AngularJS provides, attempts to parse the HTTP response body as JSON. And, it does this regardless of the HTTP status code.
If you dig into the AngularJS source code, you'll see that AngularJS sets up a promise chain, wrapped around the request to the remote server. And, in that request, you'll see that AngularJS pipes the response into a transformation method:
sendReq(config, reqData).then(transformResponse, transformResponse);
Notice that AngularJS is using the same method - transformResponse - as both the resolution handler and the rejection handler. This is because AngularJS will attempt to transform the response in the same way, regardless of whether or not the status code indicates a successful HTTP request. Now, it will still resolve all 2xx level codes and reject all non-2xx level codes; but, it will pass the same deferred value though to both handlers.
To demonstrate this, I set up a small ColdFusion API that returns different HTTP status codes based on the incoming request:
Then, I created a small AngularJS app that hits the remote API endpoint and logs out the response:
Notice that I am using the same promise handler for both the resolution and the rejection state. This is because I just want to log the response, regardless of HTTP status code. And, when I run this app and hit each end-point, I get the following output:
Notice that AngularJS parsed the JSON response value and passed it through to the promise handlers regardless of whether or not the HTTP response had a 2xx-level status code.
This is awesome; but, we still need to think about the rejection handler. In the demo, we have two different 500-level responses - one that contained JSON and one that contained a error-dump from ColdFusion. This means that, while AngularJS will parse JSON in a non-2xx level response, it doesn't mean that all responses will necessarily contain JSON. As such, you will likely need to normalize your error responses. This might entail wrapping unexpected errors in a client-side object; or, it might entail unwrapping the JSON response (as I did in the error-handler in my post on using the $http service to make AJAX requests).
Like I said above, you probably already make use of this feature in AngularJS; but, it's possible that you've never stopped to think about how this works or what the implications are. Personally, I think this is awesome and makes life just a little bit easier.
Thank you, Ben. This was not clear from the docs. Perfectly answered my question.
Awesome, glad I could help!