wondergoat77
wondergoat77

Reputation: 1815

catch errors/read status code from rest call - angular

How do you catch errors/read http status code when making a rest call in one of these formats, both work to return a successful response, just no idea how to grab the info i need. I can get the object with values returned as I need, I just cant get the http status code.

methods provided by @Claies in a response to this question (Get data from $resource response in angular factory)

$scope.makeRestCall= function () {
    $scope.member = Item.makeRestCallWithHeaders('123456789', '789456123')
    .query().$promise.then(function(response){

    });
};


 $scope.makeRestCall= function () {
    $scope.member = Item.makeRestCallWithHeaders('123456789', '789456123')
    .query({}, function() {

    })
};

I have tried to use the first method here and grab something from the function(response) such as response.status, but it returns undefined.

For reference, using this factory:

.factory("Item", function($resource) {
    var endpoint = "http://some valid url";

    function makeRestCallWithHeaders(id1, id2) {
        return $resource(endpoint, null, {
            query: {
                method: 'GET',
                headers: {
                    'id1': id1,
                    'id2': id2
                }
            }
        })
    }

    var item = {
        makeRestCallWithHeaders: makeRestCallWithHeaders
    }

    return item ;
})

Item returns something like this:

{firstName:Joe, lastName:smith}

I am really just trying to figure out how I can access the status code returned by the REST call. Absolute end goal is to read any error response and return error to UI written in angular as well. If there is a way to just read this in the UI, that could work too.

Upvotes: 0

Views: 1113

Answers (1)

lukkea
lukkea

Reputation: 3614

To read the error status you need to pass in the errorCallback to the $promise:

$scope.makeRestCall= function () {
$scope.member = Item.makeRestCallWithHeaders('123456789', '789456123')
    .query().$promise.then(
        function(response){
            //this is the successCallback
            //response.status & response.statusText do not exist here by default 
            //because you don't really need them - the call succeeded
            //see rest of answer below if you really need to do this
            //    but be sure you really do...
        },
        function(repsonse) {
            //this is the errorCallback
            //response.status === code
            //response.statusText === status text!
            //so to get the status code you could do this:
            var statusCode = response.status;
        }
    );
};

You shouldn't need the status in the successCallback, because it is a success and you know the success code implicitly.

Therefore the status is not available in the successCallback by default.

If, for some reason, you do need the status in your successCallback, you could write an interceptor to put this information somewhere, but be aware that the angular framework deals with the data differently in different success scenarios so you will need to write code for different cases.

Upvotes: 1

Related Questions