Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle errors when retrieving and processing calendars #73

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:\n\n${errorMessages.join('\n')}\nSee console for details.`;
new Notice(message);
}

return events;
}
}

async onload() {
await this.loadSettings();
Expand Down