From d053682662529a7bb6ded5208a1c9296f99a7b4a Mon Sep 17 00:00:00 2001 From: Muness Castle <931+muness@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:46:49 -0400 Subject: [PATCH 1/2] Refactor code to handle errors when retrieving and processing calendars Previously, the code did not handle errors properly when retrieving and parsing calendar data. This update introduces error handling for downloading and filtering events from the calendars. - Added error handling for downloading calendars and parsing errors. - Store error messages encountered during processing. - Notify the user about any errors encountered during processing. This change ensures that any errors encountered during the retrieval and processing of calendars are properly handled and communicated to the user. Should help with BUG: "Invalid time value" when pointing to a Nextcloud-powered ICS #61 --- src/main.ts | 77 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/src/main.ts b/src/main.ts index e8dff60..8e72797 100644 --- a/src/main.ts +++ b/src/main.ts @@ -41,45 +41,66 @@ export default class ICSPlugin extends Plugin { } async getEvents(date: string) : Promise { - var events: IEvent[] = []; - + let events: IEvent[] = []; + let errorMessages: string[] = []; // To store error messages + for (const calendar in this.data.calendars) { const calendarSetting = this.data.calendars[calendar]; - var icsArray: any[] = []; - + let icsArray: any[] = []; + + // Exception handling for downloading try { - var icsArray = parseIcs(await request({ + icsArray = parseIcs(await request({ url: calendarSetting.icsUrl })); - } catch (error) { - console.error('error retrieving calendar ' + calendarSetting.icsName + ' with ics URL ' + calendarSetting.icsUrl + ' : ' + error); - new Notice(`Error retrieving calendar with name "${calendarSetting.icsName}". See console for details.`); + console.error(`Error retrieving calendar ${calendarSetting.icsName} with ICS URL ${calendarSetting.icsUrl}: ${error}`); + errorMessages.push(`Error retrieving calendar "${calendarSetting.icsName}"`); } - const dateEvents = filterMatchingEvents(icsArray, date); - - dateEvents.forEach((e) => { - const { callUrl, callType } = extractMeetingInfo(e); - - let event: IEvent = { - utime: moment(e.start).format('X'), - time: moment(e.start).format(this.data.format.timeFormat), - endTime: moment(e.end).format(this.data.format.timeFormat), - icsName: calendarSetting.icsName, - summary: e.summary, - description: e.description, - format: calendarSetting.format, - location: e.location? e.location : null, - callUrl: callUrl, - callType: callType - }; - events.push(event); - }); + var dateEvents; + + // Exception handling for parsing and filtering + try { + dateEvents = filterMatchingEvents(icsArray, date); + + } catch (filterError) { + console.error(`Error filtering events for calendar ${calendarSetting.icsName}: ${filterError}`); + errorMessages.push(`Error filtering events in calendar "${calendarSetting.icsName}"`); + } + try { + dateEvents.forEach((e) => { + const { callUrl, callType } = extractMeetingInfo(e); + + let event: IEvent = { + utime: moment(e.start).format('X'), + time: moment(e.start).format(this.data.format.timeFormat), + endTime: moment(e.end).format(this.data.format.timeFormat), + icsName: calendarSetting.icsName, + summary: e.summary, + description: e.description, + format: calendarSetting.format, + location: e.location? e.location : null, + callUrl: callUrl, + callType: callType + }; + events.push(event); + }); + } catch (parseError) { + console.error(`Error parsing events for calendar ${calendarSetting.icsName}: ${parseError}`); + errorMessages.push(`Error parsing events in calendar "${calendarSetting.icsName}"`); + } } + + // Notify the user if any errors were encountered + if (errorMessages.length > 0) { + const message = `Encountered ${errorMessages.length} error(s) while processing calendars: ${errorMessages.join(', ')}. See console for details.`; + new Notice(message); + } + return events; - } + } async onload() { await this.loadSettings(); From 388d10f2e2dcbf43698186d28f2835cf8960fa1e Mon Sep 17 00:00:00 2001 From: Muness Castle <931+muness@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:54:57 -0400 Subject: [PATCH 2/2] refactor: Improve error message formatting The error message displayed to the user when encountering errors while processing calendars has been updated to include a new line character between each error message. This allows for better readability and clarity. The console can still be used to view more detailed error information. --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 8e72797..502f207 100644 --- a/src/main.ts +++ b/src/main.ts @@ -95,7 +95,7 @@ export default class ICSPlugin extends Plugin { // Notify the user if any errors were encountered if (errorMessages.length > 0) { - const message = `Encountered ${errorMessages.length} error(s) while processing calendars: ${errorMessages.join(', ')}. See console for details.`; + const message = `Encountered ${errorMessages.length} error(s) while processing calendars:\n\n${errorMessages.join('\n')}\nSee console for details.`; new Notice(message); }