Home > database >  Error handling - retry urlfetch on error until success
Error handling - retry urlfetch on error until success

Time:12-14

I've looked at all the relevant questions here (such as this), but still cannot make sense of this VERY simple task. So, trying to verify numbers using the NumVerify API. We're still on the free license on APILAYER so we're getting the following error from time to time

Request failed for https://apilayer.net returned code 500

I'd like to add a loop so that the script will try again until a proper response is received. Here is a snippet based on several answers here:

function numverifylookup(mobilephone) {
    console.log("input number: ",mobilephone);
    var lookupUrl = "https://apilayer.net/api/validate?access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&number=" mobilephone "&country_code=IL";
    try {
    var response = UrlFetchApp.fetch(lookupUrl);
    if (response) {//Check for truthy value
     var json = response.getContentText();
    } else {
    Utilities.sleep(2000);
    continue;//If "get" returned a falsy value then continue
  }
} catch(e) {
  continue;//If error continue looping
}
    var data = JSON.parse(response);

Sadly, still not working due to the following error:

Continue must be inside loop. (line 10

Any thoughts? I think it's actually better to solve this using muteHTTPexepctions but couldn't quite make it work. Thanks!

CodePudding user response:

I think I got this to work as below:

function numverify(mobilephone);
console.log("input number: ",mobilephone);
    var lookupUrl = "https://apilayer.net/api/validate?access_key=XXXXXXXXXXXX&number=" mobilephone "&country_code=IL";
    var i = 0;
    var trycount = 1;
    var errorcodes = "";
    while (i != 1) {
    var response = UrlFetchApp.fetch(lookupUrl, {muteHttpExceptions: true });
    var responsecode = response.getResponseCode();
    var errorcodes = errorcodes   ","   responsecode;
    if (responsecode = 200) {//Check for truthy value
      var json = response.getContentText();
      var i = 1
    } else {
      var trycount = trycount   1;
      Utilities.sleep(2000);
     }
  } 
    var data = JSON.parse(response);
    var valid = data.valid;
    var localnum = data.local_format;
    var linetype = data.line_type;
    console.log(data," ",valid," ",localnum," ",linetype," number of tries= ",trycount," responsecodes= ", errorcodes);
    var answer = [valid,localnum,linetype];
    return answer;
}

I'll circle back in case it still doesn't work. Thanks for helping!

CodePudding user response:

You cannot use continue to achieve what you want, instead you can / need to call the function again:

function numverifylookup(mobilephone) {
  console.log("input number: ", mobilephone);
  var lookupUrl = "https://apilayer.net/api/validate?access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&number="   mobilephone   "&country_code=IL";
  try {
    var response = UrlFetchApp.fetch(lookupUrl);
    if (response) {//Check for truthy value
      var json = response.getContentText();
    } else {
      Utilities.sleep(2000);
      numverifylookup(mobilephone);
    }
  } catch (e) {
    Utilities.sleep(2000);
    numverifylookup(mobilephone);//If error rerun the function
  }
  var data = JSON.parse(response);
}

As you can draw from the documentation the statement continue can only be used inside of loops, like e.g. the for loop.

  • Related