3

I just built an interceptor Service in angularJS to catch all errors from API calls to handle general errors like so:

$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
   'responseError': function(rejection) {
      alert("Something went wrong");
      return $q.reject(rejection);
    }
  };
});

Works prefectly and my server sends back this on error with a status of 409

{
message: "Email is already being used"
success: false
token: ""
}

How can access this response from the responseError interceptor?

1
  • What is available on the rejection object? Commented Jul 18, 2015 at 19:03

2 Answers 2

2

it can be done like this

$httpProvider.interceptors.push(['$q',  function($q) {
        return {
            'request': function (config) {
               //request codes
                return config;
            },
            'responseError': function(response) {

                console.log(response);
                if(response.statusText){

                    alert(response.statusText)
                }else{
                    alert("Server down")
                }
                if(response.status === 401 || response.status === 409) {
                    //response code
                }
                return $q.reject(response);
            }

        };
}]);

})

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

Comments

1
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
   'responseError': function(rejection) {
      if(rejection.status === 409) {
          //get set the error message from rejection.message/rejection.data.message and do what you want
      }
      alert("Something went wrong");
      return $q.reject(rejection);
    }
  };
});

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.