Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restangular error handling issue

I'm having an issue with restangular. This code :

Restangular.one("undefined_ressource").get().then(
    function(res){
        console.log("success", res.code);
    },
    function (res) {
        console.log("fail", res.code);
   }
);

Tries to get the url http://api.local.com/undefined_ressource which does not exits. My server returns a 404, as expected, but the fail log is never fired. Intead it goes in the success callback and logs "success 404" to the console.

Angular version 1.2.15
Restangular version 1.3.1

Any help would be welcomed! Thanks

like image 325
sbouba Avatar asked Mar 11 '14 16:03

sbouba


2 Answers

Adapted from your code example

Restangular.one("undefined_ressource").get().then(
    function(myObject){
        console.log("success obtained myObject");
    },
    function (res) {
        console.log("fail", res.status);
   }
);
  • If Restangular call is successful:

then first function should be invoked and its argument should be myObject. Unless your myObject has a specific code or status field, there won't be any added by Restangular.

  • If Restangular call fails:

then second function should be invoked and its argument be a response object with status, header, data, config fields. There shouldn't be any code field available as shown in your example.

In case of an HTTP 404 Not Found exception, in my navigator javascript console I do obtain: "fail" 404

With error interceptor

myAngularApp.run(['Restangular', '$window', function(Restangular, $window){
    Restangular.setErrorInterceptor(
        function(response) {
            if (response.status == 401) {
                console.log("Login required... ");
                $window.location.href='/login';
            } else if (response.status == 404) {
                console.log("Resource not available...");
            } else {
                console.log("Response received with HTTP error code: " + response.status );
            }
            return false; // stop the promise chain
        }
    );
}]);

If an error interceptor is set up on your angular application myAngularApp as in the above example, the failure should no more reach your custom call then second function but be processed by the errorInterceptor set up function.

In case of an HTTP 404 Not Found exception, in my navigator javascript console I do obtain: Resource not available...

Tested with:

Angular version 1.2.14
Restangular version 1.3.1
like image 65
Patrick Refondini Avatar answered Feb 14 '23 10:02

Patrick Refondini


I got the exact same symptoms as expressed in the original post above: HTTP Status OK is fine, however HTTP errors throw success.

The highest rated example didn't help at all because I didn't have an setErrorInterceptor (at the time), addResponseInterceptor doesn't trigger or errors, and finally the setErrorInterceptor I added seemed to be triggering after the promise was resolving as OK.

It turned out the problem was an error in the promise chain of my code. Make sure if you have a promise chain that each promise being generated returns the promise, else what is happening will be an earlier promise is resolving successfully and that is being returned, instead of the chained part that is failing.

Just a tip for others in case you run into this.

like image 27
Terry Chay Avatar answered Feb 14 '23 10:02

Terry Chay