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.