0

I want to implement a retry logic in my javascript code. This is how I'm calling the API:

$.ajax({
            url: api_url + 'report',
            type: 'GET',
            dataType: 'json',
            async: false,
            tryCount : 0,
            retryLimit : 3,

            headers: {
                "Authorization": "Basic " + btoa(api_username + ":" + api_pass)
            },

            data: {start: start_date, end: end_date},
            success: function(result) {
                data = result.results;
                console.log("success");
            },
            error : function(xhr, textStatus, errorThrown ) {
                console.log("in error");
                if (textStatus == 'timeout') {
                    this.tryCount++;
                    if (this.tryCount <= this.retryLimit) {
                        //try again
                        console.log("try count:");
                        console.log(this.tryCount);
                        $.ajax(this);
                        return;
                    }            
                    return;
                }
                if (xhr.status == 500) {
                    console.log("still 500");
                } else {
                    console.log("still !500");
                }
            }
        });

So when there are issues with the server and it returns http 500 then still my control in the above JS file doesn't go into the "error:" block and this line: "console.log("in error");" doesnt get printed on the console.

How can I correctly implement a retry logic in my code in case my server returns 500 then it should keep on retrying for some x amount of times?

1
  • Have you tried using statusCode? Commented Jun 13, 2016 at 5:17

1 Answer 1

1

500 error generally means that something is wrong with backend server. So it doesn't get into error block of client JavaScript. I don't think there is anything you can do. But in general you can always ask backend developers to do better error handling and return apt error response if possible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.