29

Does anyone know how you can check to see that a resource failed to be fetched in AngularJS?

For example:

//this is valid syntax
$scope.word = Word.get({ id : $routeParams.id },function() {
    //this is valid, but won't be fired if the HTTP response is 404 or any other http-error code
});

//this is something along the lines of what I want to have 
//(NOTE THAT THIS IS INVALID AND DOESN'T EXIST)
$scope.word = Word.get({ id : $routeParams.id },{
    success : function() {
      //good
    },
    failure : function() {
      //404 or bad
    }
});

Any ideas?

3 Answers 3

49

An additional callback function after your first callback function should fire when there is an error. Taken from the docs and group post:

$scope.word = Word.get({ id : $routeParams.id }, function() {
    //good code
}, function(response) {
    //404 or bad
    if(response.status === 404) {
    }
});
  • HTTP GET "class" actions: Resource.action([parameters], [success], [error])
  • non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
  • non-GET instance actions: instance.$action([parameters], [success], [error])
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is in the if statement. Do I need to write if(reponse.status == 404 ) ,if(reponse.status == 500 ), if(reponse.status == 501 ) , if .. if .. if ? Isn't this a code duplication
@Adio You can use a switch statement.
@gloopy I know about the docs, but I could not find any spec' about the input parameters passed to success- and error-callback. Like, how do I know that first param for error is response? Do you know where are they described? (apart from breakpoint and inspection)
@superjos I couldn't find the documentation but after looking at the ngResource code it looks like in the error scenario the error function gets the $http promise response object with properties data, status, headers, and config as defined here. The second parameter on error isn't used and I have edited the answer! Hope this helps.
Thanks for your further researches. I looked up again in the docs, and down below within the example it says: "It's worth noting that the success callback for get, query and other method gets passed in the response that came from the server as well as $http header getter function, so one could..."
|
5

Just to answer @Adio 's question too.

The second callback will be called when any http response code is considered to be an error by AngularJS (only response codes in [200, 300] are considered success codes). So you can have a general error handling function and don't care about the specific error. The if statement there can be used to do different actions depending on the error code, but it's not mandatory.

Comments

0

This is just to inform.

From angular 1.6.x, success and failure is deprecated. So please now follow the then and catch on behalf of success and failure.

So, the above code look like in angular 1.6.x is as below:

$scope.word = Word.get({ id : $routeParams.id }).then(=> () {
    //this is valid, but won't be fired if the HTTP response is 404 or any  other http-error code
}).catch(=> () {
    // error related code goes here
});

1 Comment

True, but note that "deprecated" to the Angular team means "completely removed". Note also that $resource without a failure handler will result in catch(reason) being called with no useful information in "reason" (empty string, null data, no other fields.) Your best/only option is to use get().$promise.then(success(), fail()) and ensure you handle the fail func. And good luck. (v1.6.2)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.