U r s u s
U r s u s

Reputation: 6978

How to process multiple errors from $http.get

I'm making multiple GET requests by building the URLs dynamically.

For any error, I want to grab the value of response.config.url, process it, and push the resulting value into an object.

The code below works fine when I only get ONE error.

When more than one error is returned, only the value from the last error is pushed into the object. I guess that's because it overwrites the previous one(s).

How do I prevent that? How do I make sure that all values get pushed into the object when there is more than one error?

(Note: annotation is an array of strings I get from an input field; _ is lodash)

function checkVariants(annotation) {
    var lemmas = annotation.text.split(' ');
    var results = [];
    var words = [];
    for (var i = 0; i < lemmas.length; ++i) {
        var urlLemmas = encodeURIComponent(lemmas[i]).toLowerCase();
        results.push(urlLemmas);
        $http({
            method: 'GET',
            url: 'https://xxxxxxx.org/variants/' + results[i] + '.txt'
        }).then(function successCallback(response) {
            console.log('Success: ', response.status)
        }, function errorCallback(response) {
            var url = response.config.url;
            words = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
            _.extend(annotation, {
                variants: words
            });
        })
    }
}

Upvotes: 0

Views: 47

Answers (2)

trincot
trincot

Reputation: 350781

Make your variants property an array and add your words to it:

$http({
    method: 'GET',
    url: 'https://wwwcoolservice.org/variants/' + results[i] + '.txt'
}).then(function successCallback(response) {
    console.log('Success: ', response.status)
}, function errorCallback(response) {
    var url = response.config.url;
    words = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));                
    if (!annotation.variants || !annotation.variants.length) { // First error: create the array
        annotation.variants = [words];
    } else { // next errors: add to the array
        annotation.variants.push(words);
    }
});

With this solution you need to detect whether you have already the variants property, so there is added value any more in calling _.extend.

Upvotes: 2

U r s u s
U r s u s

Reputation: 6978

I managed to get what I wanted.

Here's the code just for reference:

function checkVariants(annotation) {
        var lemmas = annotation.text.split(' ');
        var results = [];
        var oedVars = [];
        for (var i = 0; i < lemmas.length; ++i) {
            var urlLemmas = encodeURIComponent(lemmas[i]).toLowerCase();
            results.push(urlLemmas);
            $http({
                method: 'GET',
                url: 'https://XXXXXX.org/variants/' + results[i] + '.txt'
            }).then(function successCallback(response) {
                console.log('Success: ', response.status)
            }, function errorCallback(response) {
                var url = response.config.url;
                var words = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
                oedVars.push(words);
                _.extend(annotation, {
                    variants: oedVars
                });
            })
        }
    }

Upvotes: 0

Related Questions