From d9dd6d2807274fd20056634112c5576bab3f42b5 Mon Sep 17 00:00:00 2001 From: Chamath Wijesekera Date: Wed, 7 Feb 2024 12:11:51 -0500 Subject: [PATCH] Refactor logging for Slack API calls --- src/classes/SlackLogger.ts | 14 +++++++++++- src/listeners/commands/helpCommand.ts | 21 ++++++++++++------ src/utils/slack.ts | 31 +++++++++------------------ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/classes/SlackLogger.ts b/src/classes/SlackLogger.ts index d03453d..cf90b51 100644 --- a/src/classes/SlackLogger.ts +++ b/src/classes/SlackLogger.ts @@ -18,7 +18,7 @@ export enum LogLevel { */ export class SlackLogger { private static instance: SlackLogger; - slackClient: WebClient; + private slackClient: WebClient; /** * Singleton constructor for SlackLogger @@ -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}\`\`\``; } } diff --git a/src/listeners/commands/helpCommand.ts b/src/listeners/commands/helpCommand.ts index ee9854a..6530149 100644 --- a/src/listeners/commands/helpCommand.ts +++ b/src/listeners/commands/helpCommand.ts @@ -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 .`; -export const helpCommandHandler: Middleware = async ({ command, ack, client }) => { - await ack(); - await logCommandUsed(command); +export async function helpCommandHandler({ + command, + ack, + client, +}: SlackCommandMiddlewareArgs & AllMiddlewareArgs): Promise { + 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); + } +} diff --git a/src/utils/slack.ts b/src/utils/slack.ts index 6a97538..1112583 100644 --- a/src/utils/slack.ts +++ b/src/utils/slack.ts @@ -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 }); } } @@ -101,8 +97,7 @@ export async function getAllEmoji(client: WebClient): Promise { } 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 }); } } @@ -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; @@ -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 }); } } @@ -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, + }); } } @@ -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) {