0

I am attempting to implement UI-router resolves which will return a result even if the API call fails. Our app has back-end permissions, and if the call fails, I still need to show nested pages (which won't load if a resolve calls fails normally, unless it is wrapped into a $q promise. I implement it like this with $http:

resolve: {
    kittens: ["$q", "$timeout","$http", function ($q, $timeout, $http,) {
            var url = "some api url"
            return $http.get (url)
                .then(function(result){ return {status:true , data: result} },
                function(){ return {status:false} }); //on failure, return this
        }],
}

The above works perfectly - it returns what I need on both call success and failure, however, it seems to fail if I try it with Restangular, the code below works fine:

                        kittens: function (Restangular) {
                            return Restangular
                                .one('stuff', 999999999999)
                                .all('stuffInStuff').getList()
                                .then(function (result) {
                                    return result;
                                });
                        },

but if I try this with the above:

                                .then(function (result) {
                                    return result;
                                }, function(error){return error})

the failure doesn't return anything and the controller isn't instantiated. I don't understand why this happens. I thought both $http.get() and Restangular.one().all().getList() (as an example) are both equivalent promises, that either return a result or fail. What's the difference? How do I provide a resolve value on call fail with Restangular?

Edit: Btw, I did read this post, and I understand that if a UI-router resolve isn't wrapped, it fails if the promise is rejected, but I don't seem to fully get how to approach it with Restanagular...

edit 3: This fails as well:

                                .then(function (result) {
                                    return result;
                                })
                                .catch(function (error) {
                                    return error;
                                })
Community
  • 1
  • 1
VSO
  • 11,546
  • 25
  • 99
  • 187
  • 1
    could you try `.catch`? – Pankaj Parkar Sep 09 '15 at 21:03
  • No idea how that would work here. I only understand catch in the context of C#'s try catch. Can't say I understand $q past chaining promises and using .all. – VSO Sep 09 '15 at 21:05
  • @PankajParkar: Is catch basically Restangular's version of a second function that would normally handle the error? Edited post. – VSO Sep 09 '15 at 21:11
  • Found issue on github, you could read up on this https://github.com/mgonto/restangular/issues/451 – Pankaj Parkar Sep 09 '15 at 21:23
  • I just read that and don't see how it applies (I don't mean to be rude, I really appreciate your reply, might be just me not understanding). But from reading the link it seems that you did not to do .then(function(){return result}) before - it would execute automatically for successful calls. Now you HAVE to run the .then statement, but I *am* running it here. Maybe I am unclear on what you mean, but I read all the way through that. – VSO Sep 09 '15 at 21:29

1 Answers1

0

I think you have two ways ,

1.) Dont return restangular instance inside resolve of ui router, because you are returning promise which is rejected. you cant bypass this rejection natively. When you return Restangular.one().get() and it fails, your resolve fails. if you dont return instance, resolve will get resolved even when your request is still loading(which i think you dont want and you want wait for success/error)

  1. second , for me correct way is this.

      resolve: {
       kittens: ["$q", "Restangular", function ($q, Restangular) {
           var deferred = $q.defer();
           Restangular.one('some-api').get().then(function(data) {
               deferred.resolve(data);
           }, function(err) {
               deferred.resolve(err);
           });
           return deferred.promise;
       }],
      }
    

in this case, resolve will be resolved everytime and you will get transition even when rest call will fail

Milos Mosovsky
  • 2,913
  • 1
  • 16
  • 18