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

feat: implement notifications in error handling logic #52

Open
wants to merge 19 commits into
base: feat/grt-60-error-handling-2
Choose a base branch
from

Conversation

jahabeebs
Copy link
Collaborator

🤖 Linear

Closes GRT-59

Description

  • Adds a simple notification service to the error handling logic

@jahabeebs jahabeebs self-assigned this Sep 26, 2024
Copy link

linear bot commented Sep 26, 2024

Comment on lines 22 to 48
constructor(config: DiscordNotifierConfig) {
const intents = new IntentsBitField().add(
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
);
this.client = new Client({ intents });
this.config = config;
this.readyPromise = this.initialize();
}

/**
* Initializes the Discord notifier by logging in with the bot token and waiting for the "ready" event.
* @returns {Promise<void>} A promise that resolves when the Discord bot is ready.
* @throws {Error} If the initialization fails.
*/
private async initialize(): Promise<void> {
try {
await this.client.login(this.config.discordBotToken);
await new Promise<void>((resolve) => {
this.client.once("ready", () => {
console.log("Discord bot is ready");
resolve();
});
});
} catch (error) {
console.error("Failed to initialize Discord notifier:", error);
throw error;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i kinda don't like the any usage on context tbh although I get it jajaj, but it's unsafe (and with some stricter rules from the new template, linter wouldn't allow you to type any)
maybe we can use generics here?

cc @0xyaco @0xkenj1 thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get around context any I suppose we would have to define a context type for each error (or classify them into groups) but I think this might take a while--let's see what the others think

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's standardize the context to:

{
  request: Request,
  response?: Response,
  dispute?: Dispute,
  event?: EboEvent<EboEventName>,
  registry: EboRegistry
}

This should be enough for our use cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think I need to do this in the error handling PR rather than this notification PR though

Comment on lines 52 to 53
discordBotToken: process.env.DISCORD_BOT_TOKEN!,
discordChannelId: process.env.DISCORD_CHANNEL_ID!,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of retrieving it from env, it's better from a config or env object previously validated with Zod
the notifier is kinda like the logger, so injecting it as argument makes sense, wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI is the way to go here, I agree!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to use DI and zod

Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
Signed-off-by: jahabeebs <47253537+jahabeebs@users.noreply.github.com>
@jahabeebs jahabeebs force-pushed the feat/grt-59-implement-notifications branch from a73d337 to e03e697 Compare October 1, 2024 16:16
Copy link
Collaborator

@0xnigir1 0xnigir1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm to me now, just a few small comments & then is good to go

Comment on lines +64 to +65
DISCORD_BOT_TOKEN: envData.DISCORD_BOT_TOKEN || "",
DISCORD_CHANNEL_ID: envData.DISCORD_CHANNEL_ID || "",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the empty string mean on Discord configuration?

Comment on lines +77 to +83
private formatErrorMessage(error: Error, context: any): string {
return `**Error:** ${error.name} - ${error.message}\n**Context:**\n\`\`\`json\n${JSON.stringify(
context,
null,
2,
)}\n\`\`\``;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk if context could have a bigint field, but it will break since JSON is not compatible natively with bigints
two alternatives:

  • write a wrapper of stringify and manually handle bigint cases
  • use stringify from Viem which already handles this case

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a super nice catch.

Apparently there's a way to do this "natively" by just using a second param of JSON.stringify. This SO answer has more info about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants