Retrying UrlFetchApp by an Error using Google Apps Script (RetryFetch)

Gists

This is a sample script for retrying UrlFetchApp of Google Apps Script when an error occurs.

When the HTTP request is run using UrlFetchApp, there is a case that an error occurs in various situations. And, there is a case that when the request is run again, no error occurs. This sample script can automatically retry the requests using Google Apps Script.

Sample script

This is Class RetryFetch.

/**
 * UrlFetchApp is run by retrying when an error occurs.
 */
class RetryFetch {
  /**
   * @param {string} url URL
   * @param {object} params Object
   * @param {number} numberOfRetr Number of retry when an error occurs with the HTTP request.
   * @param {number} waitTime Wait time between the HTTP request.
   * @return {UrlFetchApp.HTTPResponse}
   */
  constructor(url, params = {}, numberOfRetry = 2, waitTime = 3) {
    this.url = url;
    if (!params.muteHttpExceptions) {
      params.muteHttpExceptions = true;
    }
    this.params = params;
    this.numberOfRetry = numberOfRetry;
    this.waitTime = waitTime;
    this.his = [];
  }

  fetch() {
    const res = UrlFetchApp.fetch(this.url, this.params);
    const statusCode = res.getResponseCode();
    this.his.push({ date: new Date(), params: this.params, statusCode });
    if (statusCode != 200 && this.numberOfRetry > 0) {
      console.log(`Status code: ${statusCode}, Retry: ${this.numberOfRetry}`);
      const idx = this.his.length - 1;
      this.his[idx].responseHeader = res.getAllHeaders();
      this.his[idx].error = res.getContentText();
      this.numberOfRetry--;
      Utilities.sleep(this.waitTime * 1000);
      this.fetch();
    } else if (this.numberOfRetry == 0) {
      return null;
    }
    return res;
  }

  /**
   * Return history of fetch requesting in this Class.
   * @return {array} History.
   */
  get history() {
    return this.his;
  }
}

This is a sample script for using Class RetryFetch.

function main() {
  const url = "sample URL";

  const rf = new RetryFetch(url);
  const res = rf.fetch();

  console.log(rf.history); // Here, you can see the history of these requests.
  console.log(res.getContentText()); // Here, UrlFetchApp.HTTPResponse is returned.
}
  • The arguments of new RetryFetch(url, params, numberOfRetry, waitTime) are as follows.

    • url: URL for requesting.
    • params: This is the same with params of fetch(url, params).
    • numberOfRetry: Number of retry. The default value is 2.
    • waitTime: Wait time between requests. The default value is 3 seconds. Unit is seconds.

Testing

When you use this script, please set url. And, please create an instance of RetryFetch. In this sample script, when the request is run without error in the 1st request, the response from the URL is returned. When an error occurs and the number of retry is over numberOfRetry, null is returned.

When you want to retrieve the history of requests, please use rf.history.

Reference

 Share!