botkit: Uncatchable throws on botbuilder-adapter-webex registerWebhookSubscription Functions
We were using botbuilder-adapter-webex and I see there was an issue where we change or added wrong tokens from Webex the application crashes. There was no way to catch the error as the throw error was after asynchronous operation and that could not be caught by try catch.
What we tried was:
const adapter = new WebexAdapter({
access_token: process.env.ACCESS_TOKEN, // access token from https://developer.webex.com
public_address: process.env.PUBLIC_ADDRESS, // public url of this app https://myapp.com/
secret: process.env.SECRET // webhook validation secret - you can define this yourself
});
adapter.registerWebhookSubscription('/api/messages'); // <- Code crashes here and try catch does not help when tokens are wrong
adapter.getIdentity();
What we tried was:
const adapter = new WebexAdapter({
access_token: process.env.ACCESS_TOKEN, // access token from https://developer.webex.com
public_address: process.env.PUBLIC_ADDRESS, // public url of this app https://myapp.com/
secret: process.env.SECRET // webhook validation secret - you can define this yourself
});
try{
adapter.registerWebhookSubscription('/api/messages'); // <- This does not help
adapter.getIdentity();
}
catch(err){
console.error(err);
}
What was the result you received?
Code crashed even with try catch.
What did you expect?
The error should be catchable on try catch or .then().catch(); so that it can be managed. If tokens expire or are changed it would simply crash the server which should not be the behavior instead it should be handled to gracefully let user decide.
Probable reason its happening:
Both registerWebhookSubscription and registerAdaptiveCardWebhookSubscription are not returning promise and throwing error asynchronously, wrapping it with a promise should fix it.
Context:
- Botkit version: any version
- Messaging Platform: Any
- Node version: Any
- Os: Any
- Any other relevant information: Try to wrap and provide solution as a pull request
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 7
- Comments: 19 (1 by maintainers)
Hi, I believe you can use the CLI flag
--unhandled-rejections=warnin order to prevent the crash while waiting for a fix.You can check the nodejs documentation here : https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
hey, the above flag is good but if webex is initiating a webhook connection, like it was for our project, then that means the webhook will fail and then an uncaught exception is thrown and essentially, your webex webhook will not work.
Instead, you can actually wait for uncaught exceptions. Add this at the entry point of your nodejs application:
Now, in case you have the same problem we did - where the webex webhook fails to initiate due to multiple server instances trying to initiate it at the same time, we implemented the retry mechanism using the RETRY_AFTER provided by the uncaught exception
Are you seeing unwanted error logs coming from the webex (npm) library?
Override your console error instance: