2

How I can set global AJAX handler which will be called only if I not already defined error handler for specific AJAX call?

Some of my ajax call really need to do some logic if error was occurred (for example re-enable button), but for some AJAX I only need to show error message if error was occurred.

For example this code is not define any error handler for AJAX call, so I want to apply to this call global error handler where I will just show error message:

user.$delete().then(function () {
    // on success
});

But this AJAX call already define error handler, and I don't want to apply global handler to it:

    $scope.deleteButtonEnabled = false;
    user.$delete().then(function () {
        // on success
    }, function(err) {   
        // Do some stuff and then show error message     
        $scope.deleteButtonEnabled = true;        
        alert('Error' + JSON.stringify(err))
    });
5
  • So ultimately you need to show the error message on every failure, in one place instead of repeating the error showing logic everywhere? Commented Sep 28, 2014 at 18:07
  • @PSL exactly. But if I have some different error handling logic I want to be able to specify it and ignore global handler for this specific call. Commented Sep 28, 2014 at 18:12
  • Are you using angular http or some thing else ?, you could make use of interceptors if you are using angular one Commented Sep 28, 2014 at 18:20
  • @PSL yes, I guess I will use something like described in this answer: stackoverflow.com/a/11972128/921193 But I don't know how to check if currently intercepting request already has a error handler Commented Sep 28, 2014 at 18:30
  • Can you show your user Service? You can handle it through config Commented Sep 28, 2014 at 18:32

1 Answer 1

5

You can make use of interceptors and some config in the http calls.

Define an interceptor:-

angular.service('HttpInterceptor', function($q){
  var service = {
     response:function(response) { //Only if you need
        //If you have to handle response data based errors you could do this here
         return $q.when(response);
      },
     responseError: function(response){
        if(!response.config.suppressGlobal){ //Handle error only if the global error handling is not suppressed
            //Handle Error based
            alert('Error' + response.status);
        }

        return $q.reject(response);
    }
  };
}).config(['$httpProvider', function($httpProvider){
    $httpProvider.interceptors.push('HttpInterceptor'); //Push the interceptor here
}]);

In your service example, when you make the http or resource call, let the method take an optional boolean argument to suppress the global error handler, pass it along with the config dictionary argument of the http call:-

   app.service('userService', function(){
        this.getData = function(suppressGlobal){ //Take a flag to suppress globals
             return  http.get('...', {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        };
        this.delete = function(suppressGlobal){ //Take a flag to suppress globals
          return http.delete('...', {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        }
        this.add = function(suppressGlobal){//Take a flag to suppress globals
            return http.post('..', data, {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        }
   });

and when you make a call:-

   // Code that needs global error handling
    user.delete().then(function () { //just call your service method as is
       // on success
    });

In some other place:-

// Code that needs to suppress global error handling
user.delete(true).then(function () { //Pass true as argument to the delete which will supress global error handling.
    // on success
}, function(err) {   
    // Do some stuff and then show error message     
    $scope.deleteButtonEnabled = true;        
    alert('Error' + JSON.stringify(err))
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.