1

I am trying to come up with a generic service that would run my http requests and provides placeholders for success and error functions. Here is my code:

 var deferred = $q.defer();
 var response = $http({
  ..............
 });
 response.then(function (data) {
        deferred.resolve(data.data);
 });
 response.catch(function (data) {
        alert('Error');
 });

And in Controller:

service.executeHTTPCall('parameters').then(successHandler);

My question is how do I provide an error handler for an executeHTTPCall call?

Thanks

Mark
  • 4,535
  • 7
  • 39
  • 76

1 Answers1

4

Well, avoid the explicit construction and things fall into place:

// no deferred, just return
return $http({..... }).then(function(data){
    // handle successes
    return data.data;
}, function(err){ // handle failures here
     alert("ERrror!!!"); // logic here
     return $q.reject(err); // this is important, like a `catch and throw` 
                            // to signal we're still handling errors.
});

But, may I suggest http interceptors instead?

$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
  return {
    'response': function(response) {
       // do something on success
    }, 
    'responseError': function(rejection) {
       // do something on failure, see docs for more usage examples here
    };
});

That would mean you can avoid wrapping every $http promise manually and also play nicer with tests.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I have never done http interceptors. May I ask for an example? – Mark Sep 16 '15 at 07:43
  • I linked to one in the answer, here: https://docs.angularjs.org/api/ng/service/$http#interceptors – Benjamin Gruenbaum Sep 16 '15 at 07:43
  • I have modified my service to your no deferred suggestion. Seems like I am getting data in success function of the service. But I am not getting into the success function when I call my service as I showed in my original code. – Mark Sep 16 '15 at 07:54
  • @Mark are you using `return` correctly there as in the docs? Also, don't use `success` and `failure`, use `then` and `catch`. – Benjamin Gruenbaum Sep 16 '15 at 07:58
  • Yes, I wasn't using the return correctly. But I am having another problem. While I am getting into controller's success handler the data there is an object but it used to be an array. I am returning data.data from the service and it's an array. – Mark Sep 16 '15 at 08:10
  • Yes, since there is more than the data to the response it resolves to an object with a `.data` property. This is why I have the `return data.data` in the wrapper example. – Benjamin Gruenbaum Sep 16 '15 at 08:11
  • Exactly what I am doing. I am returning data.data as well. response.then(function (data) { return data.data; }); response.catch(function (data) { $q.reject(data.data); }); – Mark Sep 16 '15 at 08:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89775/discussion-between-benjamin-gruenbaum-and-mark). – Benjamin Gruenbaum Sep 16 '15 at 08:14