Skip to content

Commit

Permalink
Refactor code to handle errors when retrieving and processing calendars
Browse files Browse the repository at this point in the history
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
  • Loading branch information
muness committed Oct 26, 2023
1 parent 8f14b29 commit d053682
Showing 1 changed file with 49 additions and 28 deletions.
77 changes: 49 additions & 28 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,66 @@ export default class ICSPlugin extends Plugin {
}

async getEvents(date: string) : Promise<IEvent[]> {
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();
Expand Down

0 comments on commit d053682

Please sign in to comment.