AngularJS : How to make custom Http service more robust for error handling? -


hello have simple custom angularjs service http requests:

angular.module('maxrelax')     .factory('httpsrv', function httpsrv($http, $q) {          return {             getdata: getdata         };          function getdata(method, url) {             var def = $q.defer();             $http({method:method, url:url})                 .success(function (data, status, headers, config) {                     console.log("success");                     console.log(status);                     def.resolve(data);                 })                 .error(function (data, status, headers, config) {                     console.log("error");                     geterrormessage(status);                     def.reject("failed get");                  });              return def.promise;         }          // want handle error states here , return fomratted message error          // description in case of error state (service unavailable, 404, etc..)         function geterrormessage(status) {             console.log(status);         }     }); 

which called controllers way:

httpsrv.getdata("get",app_const.mp3_actual_version_url).then(function(data) {                     console.log(data);                 }); 

i make service more robust in way of error handling. example:

httpsrv.getdata("get",app_const.mp3_actual_version_url). .success(function(data) {             // ok         }); .error(function(data) {             // call  method geterrormessage of httpsrv service formatted error message         }); 

how should update service in right way please?

thanks advice.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -