angularjs - Is it okay to handle all the $http errors in controller? -
in services, i'm invoking rest services
, returning promises
controllers. error's handled @ controllers using catch
below,
myservice.getdata(url).then(getdatasuccess).catch(exception.catcher('contact admin : '));
my question here is, since real $http
calls made @ service, should have write catchers in service or catching in controller fine?,
scenario 1: function getdata(url){ return $http.get(url); } scenario 2: (nested calls make combined results) function getotherdata(url){ var defer = $q.defer(); $http.get(url).then( function(response){ $http.get(nextservice).then( function(res){ defer.resolve('combined data'); } ) } ); return defer.promise; }
both service method not handling errors. instead returns promise. there situation kind of exception handling failed?
note: have created decorators handling javascript,angular errors , route errors separately. question particularly $http service errors.
yes have can fail triggering catch
because have no reject()
.
you using anti-pattern creating own promise , not chaining nested request properly. neither of request rejections returned anywhere.
to able chain these rid of $q.defer()
, do:
function getotherdata(url) { // return beginning of promise chain return $http.get(url).then(function (response) { // return next promise return $http.get(nextservice).then(function (res) { // combine , return data return { d1 : response.data, d2 : res.data }; }); }); }
now walk through scenarios , each part of chain intact.
think of chain each then
needs return
until end of chain
Comments
Post a Comment