-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: feat/grt-60-error-handling-2
Are you sure you want to change the base?
feat: implement notifications in error handling logic #52
Conversation
1437553
to
7ac5c81
Compare
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we can use the Static Async Factory approach from the Notion docs:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
discordBotToken: process.env.DISCORD_BOT_TOKEN!, | ||
discordChannelId: process.env.DISCORD_CHANNEL_ID!, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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>
a73d337
to
e03e697
Compare
There was a problem hiding this 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
DISCORD_BOT_TOKEN: envData.DISCORD_BOT_TOKEN || "", | ||
DISCORD_CHANNEL_ID: envData.DISCORD_CHANNEL_ID || "", |
There was a problem hiding this comment.
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?
private formatErrorMessage(error: Error, context: any): string { | ||
return `**Error:** ${error.name} - ${error.message}\n**Context:**\n\`\`\`json\n${JSON.stringify( | ||
context, | ||
null, | ||
2, | ||
)}\n\`\`\``; | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
🤖 Linear
Closes GRT-59
Description