Search code examples
javascriptandroidangularjsionic-frameworkgenymotion

$http in Ionic app not catching errors


I'm running an Ionic Android app under Genymotion emulator.

I'm having a problem with $http. When the request is successful then it's all fine. However when the request would return with a 401/403/404 code, an exception is thrown in Ionic framework: ionic.bundle.js line 17288:

"Cannot read property 'data' of undefined", response is undefined.

It seems like Ionic intercepts the responses, and 401/403/404 errors still tries to trigger the .success() callback. See attached excerpt from ionic.bundle.js:

enter image description here

Ionic Version: 1.0.0-rc.0


Solution

  • The issue was caused because I had an HTTP interceptor in my app.js. It seemed harmless but commenting it out solved the problem. Seem below what got commented out:

    //  .config(['$httpProvider', function($httpProvider) {
    //    $httpProvider.interceptors.push(function() {
    //      return {
    //        'responseError': function(rejection) {
    //          var message = "HTTP Error " + rejection.statusText + "(" + rejection.status + ") on " + rejection.config.method + " " + rejection.config.url;
    //
    //          if (typeof rejection.data !== 'undefined')
    //            message += " Error response: " + JSON.stringify(rejection.data);
    //
    //          // same as above
    //          if (typeof _errs !== 'undefined')
    //            _errs.push(new Error(message));
    //          else
    //            console.log(message);
    //        }
    //      };
    //    });
    //  }])