GAS-ICS-Sync
GAS-ICS-Sync copied to clipboard
Currently the script fails execution if it encounters a sourceCalendar URl that is not available.
This commit logs the fact that the url is available and that the syncing of the targetcalendar will be skipped and execution is allowed to continue for syncing subsequent calendars etc.
This commit is sort of a modified implementation of: https://github.com/derekantrican/GAS-ICS-Sync/pull/164
So the method is skip all syncing to that Google Calendar if any of the source calendars are unavailable? I'm not sure I like that solution - what if I have 3 ical sources for 1 Google calendar, but 1 is unavailable? Then I would stop getting updates from those other calendars.
So the method is skip all syncing to that Google Calendar if any of the source calendars are unavailable? I'm not sure I like that solution - what if I have 3 ical sources for 1 Google calendar, but 1 is unavailable? Then I would stop getting updates from those other calendars.
We can probably change the functionality to handle different cases --> right now, the scirpt just dies at that point the moment it finds a url unavailable. My little change makes it keep going since other url's correspoinding to other targets might be available.
For my use case --> i only have 1 source per target --- but multiple pairs... So I just wanted to keep going if one of the pairs fails..
but I can look into augmenting it for cases where there are multiple sources for a target and 1 or some of those sources are unavailable...
right now, the scirpt just dies at that point the moment it finds a url unavailable
That is actually a bug in the current code. This should work:
function fetchSourceCalendars(sourceCalendarURLs){
var result = []
for (var url of sourceCalendarURLs){
url = url.replace("webcal://", "https://");
callWithBackoff(function() {
var urlResponse = UrlFetchApp.fetch(url, { 'validateHttpsCertificates' : false, 'muteHttpExceptions' : true });
if (urlResponse.getResponseCode() == 200){
var urlContent = urlResponse.getContentText();
if(!urlContent.includes("BEGIN:VCALENDAR")){
Logger.log("[ERROR] Incorrect ics/ical URL: " + url);
return;
}
else{
result.push(urlContent);
Logger.log("Result: " + result.length);
return;
}
}
else{ //Throw here to make callWithBackoff run again
throw "Error: Encountered " + urlResponse.getResponseCode() + " when accessing " + url;
}
}, 5);
}
return result;
}
and
function callWithBackoff(func, maxRetries) {
var tries = 0;
var result;
while ( tries <= maxRetries ) {
tries++;
try{
result = func();
return result;
}
catch(err){
if ( err.message && (err.message.includes("is not a function") || !backoffRecoverableErrors.some(function(e){
return err.message.toLowerCase().includes(e);
}) )) {
throw err;
} else if ( err.message && err.message.includes("Forbidden") ) {
return null;
} else if ( tries > maxRetries) {
Logger.log(`Error, giving up after trying ${maxRetries} times [${err}]`);
return null;
} else {
Logger.log( "Error, Retrying... [" + err +"]");
Utilities.sleep (Math.pow(2,tries)*100) +
(Math.round(Math.random() * 100));
}
}
}
return null;
}
Closing per conversation with Jonas. This fix was pushed to master via #245 .