grammY icon indicating copy to clipboard operation
grammY copied to clipboard

bug: uncaught Exception thrown in bot.stop()

Open AB-70 opened this issue 1 year ago • 1 comments

Hello!

I just wanted to make sure the bot is stopped and disposed after a failed login attempt, and came across this issue where the bot would crash the application regardless of error handler being set or using catch().

Here's how to reproduce:

  1. Create the BotClient instance with an invalid or expired bot token.
  2. Set the error handler by using Bot.catch()
  3. Start the bot using Bot.start(). This would fail due to an invalid bot token, and the error is handled either by catch() or the default error handler.
  4. At this point, calling Bot.stop() will crash the application, even if you try to catch(). This is because the bot is not logged in and stop() function calling getUpdates()

This could also be a problem if let's say your bot token is revoked while the bot was running, and then you have to call stop(), which would result in an app crash.

I am not sure if this is a big deal but it is preventing one from gracefully handling these situations.

Solution: Adding a .catch() handler on the following line to call the default error handler might fix this. E.g.

await this.api.getUpdates({ offset, limit: 1 }).catch((err)=>{
     if(this.errorHandler) this.errorHandler(err); else throw err;
}).finally(() => this.pollingAbortController = undefined);

https://github.com/grammyjs/grammY/blob/b78c72ebec625ddf2d23eb27a9eed895710ac3c5/src/bot.ts#L481

AB-70 avatar Feb 27 '24 15:02 AB-70