Skip to content

Commit

Permalink
Refactor throwing and handling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumManiac committed Feb 5, 2024
1 parent edec0ae commit 92cee18
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
28 changes: 20 additions & 8 deletions src/classes/CalendarEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ export default class CalendarEvent {
* @param event The google calendar event to parse
* @param workspaceChannels The list of slack channels in the workspace. Used to create SlackChannel objects from the channel names in the event description
* @returns The parsed CalendarEvent object
* @throws Error if the event summary, start time, end time, or URL is undefined
*/
static fromGoogleCalendarEvent(event: calendar_v3.Schema$Event, workspaceChannels: SlackChannel[]): CalendarEvent {
if (event.summary == undefined) throw new Error("Event summary is undefined");
if (event.summary == undefined) {
throw new Error("Event summary is undefined");
}
const title = event.summary;
if (event.start?.dateTime == undefined) throw new Error("Event start is undefined");
if (event.start?.dateTime == undefined) {
throw new Error(`Event start is undefined for event ${event.summary}`);
}
const start = new Date(event.start.dateTime);
if (event.end?.dateTime == undefined) throw new Error("Event end is undefined");
if (event.end?.dateTime == undefined) {
throw new Error(`Event end is undefined for event ${event.summary}`);
}
const end = new Date(event.end.dateTime);
if (event.htmlLink == undefined) throw new Error("Event URL is undefined");
if (event.htmlLink == undefined) {
throw new Error(`Event URL is undefined for event ${event.summary}`);
}
const url = event.htmlLink;

const parsedEvent = new CalendarEvent(title, start, end, url);
Expand All @@ -58,10 +67,13 @@ export default class CalendarEvent {
parsedEvent.url = event.htmlLink ?? undefined;

if (event?.description != undefined) {
const { description, minervaEventMetadata } = parseDescription(event.description, workspaceChannels);

parsedEvent.description = description;
parsedEvent.minervaEventMetadata = minervaEventMetadata;
try {
const { description, minervaEventMetadata } = parseDescription(event.description, workspaceChannels);
parsedEvent.minervaEventMetadata = minervaEventMetadata;
parsedEvent.description = description;
} catch (error) {
throw new Error(`Failed to parse event description: ${String(error)}`);
}
}

return parsedEvent;
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ scheduleTasks(app.client, auth);
SlackLogger.getInstance().info(`Minerva has started. Deployment commit hash: ${environment.deploymentCommitHash}`);
} catch (error) {
SlackLogger.getInstance().error(
`Minerva has failed to start. Deployment commit hash: ${environment.deploymentCommitHash}:`,
`Minerva has failed to start. Deployment commit hash: \`${environment.deploymentCommitHash}\``,
String(error),
);
throw error;
}
})();
1 change: 1 addition & 0 deletions src/utils/calendarDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function parseDescriptionFromHtml(description: string): string {
* @param description The description of the event to parse
* @param workspaceChannels The Slack channels in the workspace
* @returns The parsed description and the metadata of the event that minerva uses
* @throws Error if the channel name is not specified or if the channel is not found
*/
export function parseDescription(
description: string,
Expand Down
9 changes: 7 additions & 2 deletions src/utils/googleCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OAuth2Client } from "google-auth-library";
import { google, calendar_v3 } from "googleapis";
import CalendarEvent from "../classes/CalendarEvent";
import SlackChannel from "../classes/SlackChannel";
import { SlackLogger } from "../classes/SlackLogger";

/**
* Fetches all events in the next 24 hours from the Rocketry calendar.
Expand Down Expand Up @@ -34,8 +35,12 @@ export function parseEvents(events: calendar_v3.Schema$Events, channels: SlackCh
const eventsList: CalendarEvent[] = [];
if (events.items) {
events.items.forEach((event) => {
const calendarEvent = CalendarEvent.fromGoogleCalendarEvent(event, channels);
eventsList.push(calendarEvent);
try {
const calendarEvent = CalendarEvent.fromGoogleCalendarEvent(event, channels);
eventsList.push(calendarEvent);
} catch (error) {
SlackLogger.getInstance().error(`Failed to parse Google Calendar event:`, String(error));
}
});

return eventsList;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export async function getAllSlackUsers(
* @param channelId The id of the Slack channel.
* @returns A promise that resolves to an array of SlackUserIDs in the channel, or undefined if the channel is not found.
*/
export async function getChannelMembers(client: WebClient, channelId: string): Promise<SlackUserID[] | undefined> {
export async function getChannelMembers(client: WebClient, channelId: string): Promise<SlackUserID[]> {
try {
let cursor: string | undefined = undefined;
const members: SlackUserID[] = [];
Expand All @@ -175,7 +175,7 @@ export async function getChannelMembers(client: WebClient, channelId: string): P
break;
}
}
return members.length > 0 ? members : undefined;
return members.length > 0 ? members : [];
} catch (error) {
SlackLogger.getInstance().error(`Failed to get members for channel with id \`${channelId}\`:`, String(error));
throw error;
Expand Down
11 changes: 9 additions & 2 deletions src/utils/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SlackUser, { UserType } from "../classes/SlackUser";
import { Member } from "@slack/web-api/dist/response/UsersListResponse";
import { filterSlackChannelFromName, getAllSlackChannels } from "./channels";
import { SlackUserID, getAllSlackUsers, getChannelMembers, postMessage } from "./slack";
import { SlackLogger } from "../classes/SlackLogger";

/**
* Determine the type of a Slack user based on the provided Member object.
Expand Down Expand Up @@ -58,7 +59,7 @@ export async function getAllSingleChannelGuests(slackUsers: SlackUser[]): Promis
* @returns A promise that resolves to an array of user IDs in the channel,
* or undefined if the channel is not found.
*/
export async function getAllUsersInChannel(client: WebClient, channel: string): Promise<SlackUserID[] | undefined> {
export async function getAllUsersInChannel(client: WebClient, channel: string): Promise<SlackUserID[]> {
const allSlackChannels = await getAllSlackChannels(client);
const channelId = await filterSlackChannelFromName(channel, allSlackChannels);
if (!channelId) {
Expand Down Expand Up @@ -108,7 +109,13 @@ export async function getAllSingleChannelGuestsInOneChannel(
channel: SlackChannel,
allUsersInWorkspace?: SlackUser[],
): Promise<SlackUser[]> {
const allUsersInChannel = await getAllUsersInChannel(client, channel.name);
let allUsersInChannel: SlackUserID[] = [];
try {
allUsersInChannel = await getAllUsersInChannel(client, channel.name);
} catch (error) {
SlackLogger.getInstance().error(`Failed to get users in channel \`${channel.name}\` with error:`, String(error));
throw error;
}

if (allUsersInWorkspace == undefined) {
allUsersInWorkspace = await getAllSlackUsers(client);
Expand Down

0 comments on commit 92cee18

Please sign in to comment.