javascript - Creating a function around angular $http requests -


first project in angularjs , started creating services (factories) made modular this

angular.module('app.services.public', [])     .factory('publicservice', ['$http', function publicservice($http) {         var results = {};         results.contact = function (name, email, message){              return $http.get();         };         return results;     }]); 

that call in main angular app including it. when call it, need listen success or error

publicservice.contact().success(callback).error(callback) 

my question is, i'm going doing lot of api requests through these services , seems bad code listen error everytime since 90% of time same thing.

how can create wrapper around $http.get or around factory calls?

so

apicall = function (url, data, successcallback, errorcallback){     $http.get(url,data).success(function(){         successcallback()     }).error(function(){         if(errorcallback()){ errorcallback(); return; }          // or display general error message     }) } 

i recommend against converting promise-based callback-based apis. angular adopted promises , best stay them. also, stay away $http-specific .success/.error , use promise .then/.catch apis.

how wide need cast net handle $http errors?

1) say, applies publicservice service, can "handle" @ each function:

.factory("publicservice", function($http, $q){      function handleerror(){       // invokes error handlers     }     return {        onerror: function(cb){         // register error handlers       },       dosomethinga: function(){         return $http.get("some/url/a")                     .then(function(response){                        return response.data;                     })                     .catch(function(error){                        handleerror(error);                        return $q.reject(error); // still "rethrow" error                     }       },       dosomethingb: function(){         // similar above       },       // etc...     };  }) 

then separate request error handling:

.controller("mainctrl", function($scope, publicservice){    publicservice.onerror(function(error){      $scope.showerror = true; // or    }) }) .controller("functionactrl", function($scope, publicservice){    publicservice.dosomethinga()                 .then(function(data){                   $scope.data = data;                 }); }) 

2) of course, above, apply request made via publicservice. if want catch $http errors, implement $http interceptors. won't go detail - there enough info in documentation , elsewhere - work below:

.factory("errorservice", function(){   return {     onerror: function(cb){       // register error handlers     },     broadcasterror: function(error){       // invoke error handlers     }   }; }) 

then in interceptor, use errorservice dependency:

'responseerror': function(rejection) {     errorservice.broadcasterror(rejection);     return $q.reject(rejection); } 

then handle errors globally:

.controller("mainctrl", function($scope, errorservice){    errorservice.onerror(function(error){      $scope.showerror = true; // or    }) }) 

Comments