1

Here's the scenario (AngularJS v1.3.15):

$httpProvider.interceptors.push(function($rootScope) {
  return {
    'responseError': function(rejection) {          
               //...some custom data checking

               console.log('error #1');
               return rejection;
           }
     };
});

and now, I send the query to the server using the $resource (which returns the 404 error, which is desired in that case), and I return the $promise:

var promise = controller.sendCustomQuery();

promise.catch(function (response) {
                 console.log('error #2');
               }
             );

and in the console I can only see the message error #1 and no error #2. Why ?

2
  • will error raise in console before u catch it? this case u can override window.onerror function to hook your own code Commented Jun 29, 2016 at 10:04
  • yes, the global error handler works fine, but when I want to catch the error in any custom function, it doesn't work Commented Jun 29, 2016 at 10:06

2 Answers 2

1

Instead of returning rejection itself return rejected promise

angular.module('app', [])
  .config(function($httpProvider) {
    $httpProvider.interceptors.push(function($q) {
      return {
        responseError: function(rejection) {
          console.log('error #1')
          return $q.reject(rejection)
        }
      }
    })
  })
  .run(function($http) {
    $http.get('some/error/path').catch(function() {
      console.log('error #2')
    })
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app='app'>

</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Make sure you throw/reject from responseError function:

$httpProvider.interceptors.push(function($rootScope) {
    return {
        responseError: function(rejection) {
            //...some custom data checking
            console.log('error #1');
            throw rejection;
        }
    };
});

If you just return you effectively handle exception (recover) so you will not go to catch in the next step in chain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.