angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

http success handler called when server is down

Open agent0 opened this issue 12 years ago • 10 comments

When an $http request fails because the server is down, the success handler is still called. There should be some indication that the request could not be processed (maybe also a global interceptor).

agent0 avatar Apr 19 '14 16:04 agent0

@agent0 what do you mean by "down" exactly? Not responding at all? Responding with an error HTTP code? What is your browser reporting and what is a browser being used?

pkozlowski-opensource avatar Apr 19 '14 16:04 pkozlowski-opensource

Sorry, I meant not responding at all (i.e. not running). The browser (firefox/firebug) gives me an "aborted" request on the console (whatever that means, some research on this topic did not turn up something useful).

agent0 avatar Apr 19 '14 17:04 agent0

What is the version of angularjs that you are using? Did you try with the latest released 1.3 beta?

pkozlowski-opensource avatar Apr 19 '14 18:04 pkozlowski-opensource

I'm using v1.3.0-beta.2, to be honest, I am not sure whether this is a problem of angularjs at all. I just was not able to find much information on xhr request and non responding servers at all. Maybe I am mistaken here that angular can give me some indication of this condition at all.

agent0 avatar Apr 19 '14 18:04 agent0

@agent0 hmmm, can't really reproduce it, this is what I've tried: http://plnkr.co/edit/WjnD662fc9lyoTCv6AED?p=preview

We need more info / exact scenario to progress on this one. Could you please modify my plunker to expose your use-case?

pkozlowski-opensource avatar Apr 19 '14 18:04 pkozlowski-opensource

Hm, I can't reproduce it directly with this example, either. I will look into it and come back to you later...

agent0 avatar Apr 19 '14 19:04 agent0

Ok, I found out how to reproduce the situation, unfortunately I am not that deep into angularjs yet that I could easily modify the example accordingly. The problem seems to be related to route changes as it only occurs when I press a navbar button that triggers a route change to a page who's controller loads the data from the server. Here is my code

app.js:
      }).when('/tagcloud', {
        templateUrl : 'views/TagCloud/TagCloud.html'
      }).when('/highscores', {

TagCloud.html:
    <div ng-controller="TagCloudCtrl">

TagCloudCtrl:

    $scope.load = function() {
      TagService.getTagCount().then(function(resp){
        $scope.tagCountList = resp.data.tagCounts;
        $scope.createCloud();
      });                      
    }

    $scope.load();

TagService:

    this.getTagCount = function(){
      console.log("TagService::getTagCount");
      var promise = $http({method: 'GET', url: 'api/tag/count'});
      return promise;      
    }

With this code, I call my app, log in and select a random route. Then I shut down the server and call the #/tagcloud route. Then the promise is resolved to the success case...

agent0 avatar Apr 19 '14 19:04 agent0

@agent0 I'm afraid that it will be very, very hard to progress on this one without more details... The only thing that comes to my mind is that you've got a $http interceptor somewhere that misbehaves on error results. But this is just speculation.

Try to narrow down your problem and post a live reproduce scenario - otherwise this issue risks being left out as not actionable....

pkozlowski-opensource avatar Apr 20 '14 12:04 pkozlowski-opensource

I think I have to apologize on this one. Your hint with the interceptor pointed to the error. Here is the code of my (erroneous) interceptor:

      $httpProvider.interceptors.push(function($q, $location, $rootScope, $cookies) {
        return {
          'responseError' : function(response) {
            if(response.status == 401){
              console.log("not authorized");

              $rootScope.sessionId = null;        
              $cookies.sessionId = "";

              $location.path('/public/login');
              return $q.reject(response);
            } else {
              response;
            }
          }
        };
      });

If I change the last line to return $q.reject(response); the error handler of the request is called. I will have to go over the documentation for interceptors again to understand my error here. Thank you for your time and patience.

agent0 avatar Apr 20 '14 13:04 agent0

@agent0 no worries, glad it is resolved. Going to close this one as indeed this interceptor doesn't look good. Keep in mind that handling a rejection in an interceptor chain is like catching an exception and handling it in the synchronous programming.

pkozlowski-opensource avatar Apr 20 '14 13:04 pkozlowski-opensource