3

I expected that bad requests would fall in the function defined inside $http#error, but that does not happen. Instead I had to create an interceptor. And even though I know the status code should be 404, the returned status code in the response is 0.

 $http({url: "myurl", method: "GET"}).success(
   function(data, status, headers, config) {
     if (data.ret_code === 200) {
        //do something
     }
   }).error(function(data, status, headers, config) {
     //NEVER GETS HERE ON BAD REQUEST!!
   });

and here is the interceptor:

var interceptor = ['$q', function ($q) {

  function success(response) {
    return response;
  }

  function error(response) {
    //ALWAYS 0!
    var status = response.status;

    return $q.reject(response);
  }

  return function (promise) {
    return promise.then(success, error);
  }
}];

1-Why is this? 2-Is there a way to catch bad requests in $http#error? 3-Is there a way to get the actual 404 status code?

1
  • Is it hitting your server? Commented Jul 24, 2014 at 0:26

2 Answers 2

1

I know of a "bug" in Android where files delivered from the browser cache give a statuscode 0.

Github Issue: https://github.com/angular/angular.js/issues/1720

Hacked-up Fix (from the same page):

// fix status code for file protocol (it's always 0) and 0 status code for http(s) protocol on Android devices
status = (protocol == 'file') ? (response ? 200 : 404) : (response && status == 0 && window.navigator.userAgent.indexOf('Android') !== -1 ? 200 : status);
Sign up to request clarification or add additional context in comments.

1 Comment

0

You can pass $rootScope to interceptor and update your error function something like this

function error(response) {
    if (response.status === 0) {
       $rootScope.errorStatus = 'No connection. Verify application is running.';
    } else if (response.status == 401) {
       $rootScope.errorStatus = 'Unauthorized';
    } else if (response.status == 405) {
       $rootScope.errorStatus = 'HTTP verb not supported [405]';
    } else if (response.status == 500) {
       $rootScope.errorStatus = 'Internal Server Error [500].';
    } else {
      $rootScope.errorStatus = JSON.parse(JSON.stringify(response.data));
    }
   return $q.reject(response);}

You can use errorStatus object to display your error details.

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.