Skip to content

Instantly share code, notes, and snippets.

@naosim
Created July 16, 2019 22:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naosim/8fc6033b6f92e426eddc0424e7f7aa71 to your computer and use it in GitHub Desktop.
Save naosim/8fc6033b6f92e426eddc0424e7f7aa71 to your computer and use it in GitHub Desktop.
【GAS】リトライありのfetch
function retryFetch(url) {
var lastError = null;
for(var i = 0; i < 3; i++) {
try {
var res = UrlFetchApp.fetch(url)
if(res.getResponseCode() == 200) {
return res.getContentText("UTF-8")
} else {
lastError = 'HTTPステータスコードが200以外: ' + res.getResponseCode() + ', ' + url;
}
} catch(e) {
lastError = e;
Logger.log(e);
}
Utilities.sleep(3000);
}
throw lastError + ', ' + url
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment