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
Post a Comment