2

I've been tracking down the reason why I cannot see the response from my webapi in angularjs using $http if the status code is less than 200 and greater than 300.

I've debugged angular.js to the point where I understand whats happening (basically its discarding the promise that contains the data i care about and creating a new one without it if the status codes are not deemed a success)

-see code at https://github.com/angular/angular.js/blob/master/src/ng/q.js#L270-L280

this.$$state.value (holds the http respone) is lost at this point for some reason.

Does it make sense to omit the original values? I'm not going to submit a bug report without asking here if I'm right or wrong.

The reason behind all this is that I am processing some credit card info on the server side. If the processor says its an invalid card, I wouldn't think it would be a 200 code, would it? See the answer to this question..... Suggesting 400 for business rules. This still fails in angularjs.

What is the appropriate HTTP status code response for a general unsuccessful request (not an error)?

Also, FYI, httpInterceptors do not work either, since they are utilized after this 'promise replacement' occurs.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
CarComp
  • 1,929
  • 1
  • 21
  • 47
  • I'm not sure how repeating your title at the end of your question makes this a really good question for Stack Overflow. – TylerH Jan 06 '16 at 16:52
  • This isn't the only http client which does that : I had a similar problem with Oracle Service Bus' http connector which discarded responses for some codes. It is an optimization where they consider error messages for some codes to be irrelevant. – Aaron Jan 06 '16 at 16:54
  • 3
    The portion of the Angular code you mention has absolutely nothing to do with HTTP status codes. – Pointy Jan 06 '16 at 16:56
  • 2
    Pretty sure you meant to cite [isSuccess](https://github.com/angular/angular.js/blob/master/src/ng/http.js#L241-L243) in http.js. – Brad Christie Jan 06 '16 at 17:01
  • Yes I know, i'm sure i've said something wrong here. These are the hazards of posting about stuff you aren't too sure about. And yes, it is isSuccess that does this, however I am not assuming everyone who reads this knows the source code of angular.js. – CarComp Jan 06 '16 at 17:04
  • 1
    Angular's $http returns the data received regardless the status code. Show us some code how do you use it. – Tamas Hegedus Jan 06 '16 at 17:35

2 Answers2

1

From the Docs:

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called.

— AngularJS $http Service API Reference - General Usage

The $http service rejects responses outside of the range of 200 to 299 but it does not "discard the message response". Both the resolve and the reject methods of the $http promise are invoked with the entire response object.

This means all of the data is available in the rejection handler.

var dataPromise = $http(configObject)
    .then (function onFulfilled(response) {
        //return data for chaining
        return response.data;
     })
     .catch (function onRejected(response) {
         if (response.status == 400) {
             console.log("400 Bad Request");
             console.log("REASON:", response.data);
             //chain with default data
             return defaultData;
         } else {
             //throw to chain rejection
             throw response;
         }
    });

In the above example, the rejection handler logs the response for messages with status 400 and converts the rejection to fulfilled with default data. All other status responses get chained as a rejection.

Data is not discarded; it is available and can be used in the rejection handler.

For more information on chaining promises, see Angular execution order with $q.

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95
0

Do you write status codes when you use promise callbacks then, catch and finally after the $resource call.

Here is what I would check for :

MyRestAPI.XXX({resource}, {data}, MyCallbackFunction).$promise.catch(function(response) {
    //should be called upon error
    //check response.status for HTTP code.
}).then(function() {
     // OK 
});
João Cunha
  • 3,776
  • 3
  • 22
  • 33
Abhay
  • 1
  • 1