From c06fb4b9cd846210e71d71fa07b5279fa406cad8 Mon Sep 17 00:00:00 2001 From: Paul Woolcock <11843015+phw198@users.noreply.github.com> Date: Sun, 1 Nov 2020 17:11:58 +0000 Subject: [PATCH] Handle null being returned from GetCalendarEntriesInRecurrence() #1082 --- .../GoogleOgcs/GoogleCalendar.cs | 4 +- src/OutlookGoogleCalendarSync/Recurrence.cs | 72 ++++++++++--------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs b/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs index 86b15f29..b1cdc28e 100644 --- a/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs +++ b/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs @@ -186,8 +186,8 @@ public List GetCalendarEntriesInRecurrence(String recurringEventId) { return result; } catch (System.Exception ex) { - Forms.Main.Instance.Console.Update("Failed to retrieve recurring events", Console.Markup.error); - log.Error(ex.Message); + Forms.Main.Instance.Console.UpdateWithError("Failed to retrieve recurring events", OGCSexception.LogAsFail(ex)); + OGCSexception.Analyse(ex); return null; } } diff --git a/src/OutlookGoogleCalendarSync/Recurrence.cs b/src/OutlookGoogleCalendarSync/Recurrence.cs index 2f9c403a..7ed9086a 100644 --- a/src/OutlookGoogleCalendarSync/Recurrence.cs +++ b/src/OutlookGoogleCalendarSync/Recurrence.cs @@ -438,6 +438,8 @@ private Event getGoogleInstance(ref Microsoft.Office.Interop.Outlook.Exception o log.Debug("Google exception event is not cached. Retrieving all recurring instances..."); } List gInstances = GoogleOgcs.Calendar.Instance.GetCalendarEntriesInRecurrence(gRecurringEventID); + if (gInstances == null) return null; + //Add any new exceptions to local cache googleExceptions = googleExceptions.Union(gInstances.Where(ev => !String.IsNullOrEmpty(ev.RecurringEventId))).ToList(); foreach (Event gInst in gInstances) { @@ -524,47 +526,47 @@ public static void CreateGoogleExceptions(AppointmentItem ai, String recurringEv log.Debug("Creating Google recurrence exceptions."); List gRecurrences = GoogleOgcs.Calendar.Instance.GetCalendarEntriesInRecurrence(recurringEventId); - if (gRecurrences != null) { - RecurrencePattern rp = null; - Exceptions excps = null; - try { - rp = ai.GetRecurrencePattern(); - excps = rp.Exceptions; - for (int e = 1; e <= excps.Count; e++) { - Microsoft.Office.Interop.Outlook.Exception oExcp = null; - try { - oExcp = excps[e]; - for (int g = 0; g < gRecurrences.Count; g++) { - Event ev = gRecurrences[g]; - DateTime gDate = ev.OriginalStartTime.DateTime ?? DateTime.Parse(ev.OriginalStartTime.Date); - Boolean isDeleted = exceptionIsDeleted(oExcp); - if (isDeleted && !ai.AllDayEvent) { //Deleted items get truncated?! - gDate = gDate.Date; - } - if (oExcp.OriginalDate == gDate) { - if (isDeleted) { - Forms.Main.Instance.Console.Update(GoogleOgcs.Calendar.GetEventSummary(ev), Console.Markup.calendar); - Forms.Main.Instance.Console.Update("Recurrence deleted."); - ev.Status = "cancelled"; - GoogleOgcs.Calendar.Instance.UpdateCalendarEntry_save(ref ev); - } else { - int exceptionItemsModified = 0; - Event modifiedEv = GoogleOgcs.Calendar.Instance.UpdateCalendarEntry(oExcp.AppointmentItem, ev, ref exceptionItemsModified, forceCompare: true); - if (exceptionItemsModified > 0) { - GoogleOgcs.Calendar.Instance.UpdateCalendarEntry_save(ref modifiedEv); - } + if (gRecurrences == null) return; + + RecurrencePattern rp = null; + Exceptions excps = null; + try { + rp = ai.GetRecurrencePattern(); + excps = rp.Exceptions; + for (int e = 1; e <= excps.Count; e++) { + Microsoft.Office.Interop.Outlook.Exception oExcp = null; + try { + oExcp = excps[e]; + for (int g = 0; g < gRecurrences.Count; g++) { + Event ev = gRecurrences[g]; + DateTime gDate = ev.OriginalStartTime.DateTime ?? DateTime.Parse(ev.OriginalStartTime.Date); + Boolean isDeleted = exceptionIsDeleted(oExcp); + if (isDeleted && !ai.AllDayEvent) { //Deleted items get truncated?! + gDate = gDate.Date; + } + if (oExcp.OriginalDate == gDate) { + if (isDeleted) { + Forms.Main.Instance.Console.Update(GoogleOgcs.Calendar.GetEventSummary(ev), Console.Markup.calendar); + Forms.Main.Instance.Console.Update("Recurrence deleted."); + ev.Status = "cancelled"; + GoogleOgcs.Calendar.Instance.UpdateCalendarEntry_save(ref ev); + } else { + int exceptionItemsModified = 0; + Event modifiedEv = GoogleOgcs.Calendar.Instance.UpdateCalendarEntry(oExcp.AppointmentItem, ev, ref exceptionItemsModified, forceCompare: true); + if (exceptionItemsModified > 0) { + GoogleOgcs.Calendar.Instance.UpdateCalendarEntry_save(ref modifiedEv); } - break; } + break; } - } finally { - oExcp = (Microsoft.Office.Interop.Outlook.Exception)OutlookOgcs.Calendar.ReleaseObject(oExcp); } + } finally { + oExcp = (Microsoft.Office.Interop.Outlook.Exception)OutlookOgcs.Calendar.ReleaseObject(oExcp); } - } finally { - excps = (Exceptions)OutlookOgcs.Calendar.ReleaseObject(excps); - rp = (RecurrencePattern)OutlookOgcs.Calendar.ReleaseObject(rp); } + } finally { + excps = (Exceptions)OutlookOgcs.Calendar.ReleaseObject(excps); + rp = (RecurrencePattern)OutlookOgcs.Calendar.ReleaseObject(rp); } }