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;
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
Patrick refondini
source share