grammY
grammY copied to clipboard
bug: uncaught Exception thrown in bot.stop()
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:
- Create the BotClient instance with an invalid or expired bot token.
- Set the error handler by using
Bot.catch()
- Start the bot using
Bot.start()
. This would fail due to an invalid bot token, and the error is handled either bycatch()
or the default error handler. - At this point, calling
Bot.stop()
will crash the application, even if you try tocatch()
. This is because the bot is not logged in and stop() function callinggetUpdates()
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