-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement notifications in error handling logic (#52)
Showing
21 changed files
with
5,249 additions
and
6,445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { z } from "zod"; | ||
|
||
const ConfigSchema = z.object({ | ||
DISCORD_BOT_TOKEN: z.string().min(1), | ||
DISCORD_CHANNEL_ID: z.string().min(1), | ||
}); | ||
|
||
export const config = ConfigSchema.parse(process.env); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/automated-dispute/src/services/discordNotifier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { ILogger } from "@ebo-agent/shared"; | ||
import { Client, IntentsBitField, TextChannel } from "discord.js"; | ||
import { stringify } from "viem"; | ||
|
||
import { NotificationService } from "./notificationService.js"; | ||
|
||
interface DiscordNotifierConfig { | ||
discordBotToken: string; | ||
discordChannelId: string; | ||
} | ||
|
||
/** | ||
* A notifier class for sending error notifications to a Discord channel. | ||
*/ | ||
export class DiscordNotifier implements NotificationService { | ||
private client: Client; | ||
private config: DiscordNotifierConfig; | ||
private logger: ILogger; | ||
|
||
private constructor(client: Client, config: DiscordNotifierConfig, logger: ILogger) { | ||
this.client = client; | ||
this.config = config; | ||
this.logger = logger; | ||
} | ||
|
||
/** | ||
* Creates an instance of the DiscordNotifier. | ||
* @param {DiscordNotifierConfig} config - The configuration object for the DiscordNotifier. | ||
* @param {ILogger} logger - The logger instance. | ||
* @returns {Promise<DiscordNotifier>} A promise that resolves to a DiscordNotifier instance. | ||
*/ | ||
public static async create( | ||
config: DiscordNotifierConfig, | ||
logger: ILogger, | ||
): Promise<DiscordNotifier> { | ||
const intents = new IntentsBitField().add( | ||
IntentsBitField.Flags.Guilds, | ||
IntentsBitField.Flags.GuildMessages, | ||
); | ||
const client = new Client({ intents }); | ||
|
||
try { | ||
await client.login(config.discordBotToken); | ||
await new Promise<void>((resolve) => { | ||
client.once("ready", () => { | ||
logger.info("Discord bot is ready"); | ||
resolve(); | ||
}); | ||
}); | ||
} catch (error) { | ||
logger.error(`FFailed to initialize Discord notifier: ${error}`); | ||
throw error; | ||
} | ||
|
||
return new DiscordNotifier(client, config, logger); | ||
} | ||
|
||
/** | ||
* Sends an error notification to the specified Discord channel. | ||
* @param {Error} error - The error to notify about. | ||
* @param {any} context - Additional context information. | ||
* @returns {Promise<void>} A promise that resolves when the message is sent. | ||
*/ | ||
public async notifyError(error: Error, context: any): Promise<void> { | ||
try { | ||
const channel = await this.client.channels.fetch(this.config.discordChannelId); | ||
if (!channel || !channel.isTextBased()) { | ||
throw new Error("Discord channel not found or is not text-based"); | ||
} | ||
const errorMessage = this.formatErrorMessage(error, context); | ||
await (channel as TextChannel).send(errorMessage); | ||
this.logger.info("Error notification sent to Discord"); | ||
} catch (err) { | ||
this.logger.error(`Failed to send error notification to Discord: ${err}`); | ||
} | ||
} | ||
|
||
/** | ||
* Formats the error message to be sent to Discord. | ||
* @param {Error} error - The error object. | ||
* @param {any} context - Additional context information. | ||
* @returns {string} The formatted error message. | ||
*/ | ||
private formatErrorMessage(error: Error, context: unknown): string { | ||
return `**Error:** ${error.name} - ${error.message}\n**Context:**\n\`\`\`json\n${stringify( | ||
context, | ||
null, | ||
2, | ||
)}\n\`\`\``; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.