Bug fix problem

I have a problem with restangular. This code:

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

Trying to get url http://api.local.com/undefined_ressource , which does not exit. My server returns 404, as expected, but the crash log never starts. Intead it goes into the success callback and the "success 404" logs to the console.

 Angular version 1.2.15 Restangular version 1.3.1 

Any help would be appreciated! Thanks

+7
error-handling restangular
source share
3 answers

Adapted from your sample code

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

then the first function should be called and its argument should be myObject . If your myObject does not have a special code or status field, the addition of Restangular will not be added.

  • If the restatular call fails:

then the second function should be called, and its argument is the response object with status, header, data, configuration fields. Your example should not have a code field, as shown in your example.

In the case of an HTTP 404 Not Found exception , in my navigator javascript console I get: "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 your error angler is configured in your angular myAngularApp application, as in the example above, the failure should not exceed your custom call, and then the second function, but it is handled by setting the errorInterceptor function.

In the case of an HTTP 404 Not Found exception , in my navigator javascript console I get: Resource unavailable ...

Tested with

 Angular version 1.2.14 Restangular version 1.3.1 
+14
source share

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

The highest rating did not help, because I did not have setErrorInterceptor (at that time), addResponseInterceptor did not start or did not cause errors, and finally, the added setErrorInterceptor seems to start after the promise was resolved as OK.

It turned out that the problem was due to an error in the promise chain of my code. Make sure you have a chain of promises that each generated promise returns a promise, otherwise what happens will be an earlier promise that is successfully resolved and that is returned, and not the chain that fails.

Just tell others if you come across this.

+1
source share

I only got this to work by adding an interceptor to $httpProvider in my configuration in addition to using my configuration for RestangularProvider

src - https://docs.angularjs.org/api/ng/service/ $ http # interceptors

 // HttpConfig.js function( $httpProvider ) { $httpProvider.interceptors.push( function( $q, $rootScope ) { return { 'request': function( config ) { return config; }, 'requestError': function( rejection ) { return rejection; }, 'response': function( response ) { return response; }, //THIS is what makes it work for me 'responseError': function( rejection ) { return $q.reject( rejection ); } }; } ); } 
0
source share

All Articles