Skip to content

Commit

Permalink
Refactor logging for Slack API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumManiac committed Feb 7, 2024
1 parent fef8000 commit d9dd6d2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
14 changes: 13 additions & 1 deletion src/classes/SlackLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum LogLevel {
*/
export class SlackLogger {
private static instance: SlackLogger;
slackClient: WebClient;
private slackClient: WebClient;

/**
* Singleton constructor for SlackLogger
Expand Down Expand Up @@ -125,6 +125,18 @@ export class SlackLogger {
* @returns The formatted code block content
*/
private static formatCodeBlockContent(codeBlockContent: unknown): string {
if (codeBlockContent instanceof Error) {
// Recursively get the causes of the error
let errorContent = `${codeBlockContent}`;
let cause = (codeBlockContent as Error).cause;
while (cause) {
errorContent += `\t[cause]: ${cause}`;

cause = (cause as Error)?.cause;
}
return `\`\`\`${errorContent}\`\`\``;
}

return `\`\`\`${codeBlockContent}\`\`\``;
}
}
21 changes: 15 additions & 6 deletions src/listeners/commands/helpCommand.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Middleware, SlackCommandMiddlewareArgs } from "@slack/bolt";
import { AllMiddlewareArgs, SlackCommandMiddlewareArgs } from "@slack/bolt";
import { logCommandUsed } from "../../utils/logging";
import { postEphemeralMessage } from "../../utils/slack";
import { SlackLogger } from "../../classes/SlackLogger";

const message = `For more information, check out <https://github.com/waterloo-rocketry/minerva-rewrite/blob/main/README.md|minerva's README>.`;

export const helpCommandHandler: Middleware<SlackCommandMiddlewareArgs> = async ({ command, ack, client }) => {
await ack();
await logCommandUsed(command);
export async function helpCommandHandler({
command,
ack,
client,
}: SlackCommandMiddlewareArgs & AllMiddlewareArgs): Promise<void> {
ack();
logCommandUsed(command);

await postEphemeralMessage(client, command.channel_id, command.user_id, message);
};
try {
await postEphemeralMessage(client, command.channel_id, command.user_id, message);
} catch (error) {
SlackLogger.getInstance().error("Failed to send help message", error);
}
}
31 changes: 10 additions & 21 deletions src/utils/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ export async function postEphemeralMessage(

return res;
} catch (error) {
SlackLogger.getInstance().error(
`Failed to post ephemeral message to user \`${user}\` in channel \`${channel}\` with error:`,
error,
);
throw error;
throw Error(`Failed to post ephemeral message to user \`${user}\` in channel \`${channel}\``, { cause: error });
}
}

Expand All @@ -101,8 +97,7 @@ export async function getAllEmoji(client: WebClient): Promise<string[]> {
}
return Object.keys(result.emoji);
} catch (error) {
SlackLogger.getInstance().error(`Failed to get emojis for workspace:`, error);
throw error;
throw Error("Failed to get emojis for workspace", { cause: error });
}
}

Expand Down Expand Up @@ -141,8 +136,7 @@ export async function getAllSlackUsers(
break;
}
} catch (error) {
SlackLogger.getInstance().error(`Failed to get users from workspace:`, error);
throw error;
throw Error("Failed to get users from workspace", { cause: error });
}
}
return usersList;
Expand Down Expand Up @@ -177,8 +171,7 @@ export async function getChannelMembers(client: WebClient, channelId: string): P
}
return members.length > 0 ? members : [];
} catch (error) {
SlackLogger.getInstance().error(`Failed to get members for channel with id \`${channelId}\`:`, error);
throw error;
throw Error(`Failed to get members for channel with id \`${channelId}\``, { cause: error });
}
}

Expand Down Expand Up @@ -207,11 +200,9 @@ export function addReactionToMessage(
timestamp: timestampStr,
});
} catch (error) {
SlackLogger.getInstance().error(
`Failed to add reaction \`${emoji}\` to message \`${timestampStr}\` in \`${channel}\`:`,
error,
);
throw error;
throw Error(`Failed to add reaction \`${emoji}\` to message \`${timestampStr}\` in \`${channel}\``, {
cause: error,
});
}
}

Expand All @@ -234,11 +225,9 @@ export async function getMessagePermalink(
message_ts: timestamp,
});
} catch (error) {
SlackLogger.getInstance().error(
`Error fetching message permalink for message with timestamp \`${timestamp}\` in \`${channel}\`:`,
error,
);
throw error;
throw Error(`Failed to get permalink for message with timestamp \`${timestamp}\` in \`${channel}\``, {
cause: error,
});
}

if (res?.ok) {
Expand Down

0 comments on commit d9dd6d2

Please sign in to comment.