0

In a typical AJAX call,what is the way to make the error handling more meaningful.
A template code as below does not help in conveying anything.
How do we decipher the exception better?

<script>
var nameapp=angular.module('countryListApp',[]);
nameapp.controller('CountryListCtrl',function($scope,$http){
     var responsePromise = $http.get("/catalog/countries.json");
     responsePromise.success(function(data, status, headers, config){$scope.countries=data; });
     responsePromise.error(function(data, status, headers, config) {
        console.log("AJAX has failed"); 
    }); 

});
2
  • 1
    you could create your own httpInterceptor that will have watch on each request Commented May 4, 2015 at 16:43
  • that's horrible to read. Why do you need variables and not just chain the methods? Far easier to read when chained. Also not hard to create a common error handler for many requests and pass it as reference Commented May 4, 2015 at 16:49

1 Answer 1

1

I use $httpProvider.interceptors for this, in your configuration you can set up global error handling. For most of my stuff MyHttpErrorHandler would result in a toast.

app.config(['$httpProvider', function($httpProvider )
{
    $httpProvider.interceptors.push(function($q) {
      return {


        // optional method
        'response': function(response) {
          return response;
        },

        // optional method
       'responseError': function(rejection) {

            // window.informUser('Error '+rejection.status +': ' +rejection.config.url);
            if(rejection.status === 0)
            {

                if(window.isInWrapper)
                {
                    //Logout if in an app;
                    window.location.href='ios:logout';
                }
                else
                {
                    window.location.reload();
                }                   
            }


          return $q.reject(rejection);
        }
      };
    });

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

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.