简体   繁体   中英

Retry try catch in Google Apps Script

When processing results of Google forms with Google Apps Script accessing the form by …

    let formID = FormApp.getActiveForm().getId();

… sometimes fails with an exception like "Form data could not be retrieved." Manually started just a minute later it works properly.

To handle those errors the best way I want to catch the exception and retry the method one minute later. I came up with this:

    function foo() {
      const maxTries = 3;
      let formID;
      let tries = 1;
      while(true) {
        try {
          formID = FormApp.getActiveForm().getId();
          break;
        } catch (e) {
          console.log("Retrieving form data failed (" + tries + ")");
          if (tries >= maxTries) {
            console.log("Retrieving form data not possible"); // and/or …
            throw(e);
            return;
          } else {
            tries++;
          };
        };
      };
      // Do things with form stuff
    };

How can I insert a 60 second pause between the tries? And I'm not sure anyway, if there isn't a better way to overcome those errors (at all or within Google Apps Script).

In this case why don't you try using Utilities.sleep . This method will pretty much puts the script to sleep for a set amount of time. Please note the maximum amount is of 5 minutes as the maximum execution time for a script is of 6 minutes. You can check runtime limits in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM