Created
July 16, 2019 22:12
-
-
Save naosim/8fc6033b6f92e426eddc0424e7f7aa71 to your computer and use it in GitHub Desktop.
【GAS】リトライありのfetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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