Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Uncatchable throws on botbuilder-adapter-webex registerWebhookSubscription Functions #2217

Open
punithk opened this issue May 20, 2022 · 19 comments

Comments

@punithk
Copy link

punithk commented May 20, 2022

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
@Dayavats
Copy link

facing similar issue while using this package in case of wrong token

@pgoldweic
Copy link

I've also faced a similar issue although not with botbuilder-adapter-webex, but with the built-in botkit adapter used for Microsoft Webchat integration, specifically when specifying a wrong token. @benbrown suggested a work-around which I haven't had a chance to implement yet (#2200 (comment)). The idea was to create custom webhook endpoints that would allow one to catch exceptions. Not sure if this might help you too. In any case, I wonder if 'promisifying' as suggested by @punithk could be applied to both situations, and be a better solution that requiring each botkit user to resolve via customization?

@Akanksha-270392
Copy link

Akanksha-270392 commented May 24, 2022

Facing same issue in case of wrong token. Please suggest some solution.

@VaniKaushik-2511
Copy link

While using this package facing issue in case of wrong token.

@Maleeha456
Copy link

Facing an issue with this package when the token is changed. It crashes the application.

@Sayak-Bhattacharjee
Copy link

I have also faced the same issue, the application crashes if the token has been altered

@Dhananjay220398
Copy link

Facing same issue in case of wrong token. Please suggest some solution.

@Adishjain58
Copy link

facing similar issue while using this package in case of wrong token

@Akash2695
Copy link

I am also facing the similar issue while using this package in case of wrong token.

@rashmi0403
Copy link

Facing an issue with this package when the token is changed. It crashes the application.

@Sakshi21197
Copy link

Hi @benbrown while using this botbuilder-webex-adaptor in my project whenever token is altered or changes it is causing my system to crash could you provide any solution.

@Dhairya-Kapoor
Copy link

facing similar issue while using this package. Please suggest some solution.

@Deekshashandilya
Copy link

issue while using this package

@benbrown
Copy link
Contributor

Did something change in Webex that caused this issue to occur more often... or are you all from the same company?

@pgoldweic
Copy link

My guess is that, just like it happened to me in issue #2200, developers are probably updating their node installations, which is the cause for the trouble (newer versions of node do not accept uncaught exceptions , and this causes the bot script to exit)

@punithk
Copy link
Author

punithk commented Jul 11, 2022

@benbrown issue is that above code crashes if wrong credentials are passed and when we try to create a cycling credentials and some wrong credentials get in, due to which entire server crashes and does not allow to go any further. Other services in that server too does not run as the adapter invocation happens on the main flow.

Issue is that error is thrown probably internally on a promise but the registerWebhookSubscription is not promisfied due to which we cannot catch this error and instead crashes the server.

i have made the changes on this, and I see there are many such places where there are error thrown and since they are on promise we cannot catch them due to which it crashes.

The changes made in my pull request is made in such a way that it wouldn't break existing code as well.

So kindly check and let us know.

@ThibaudL
Copy link

ThibaudL commented Aug 3, 2023

Hi, I believe you can use the CLI flag --unhandled-rejections=warn in 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

@penguinsource
Copy link

penguinsource commented Mar 19, 2024

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:

process
  .on('unhandledRejection', (reason, promise) => {
    if (reason.message.includes('WEBEX_TRACKING_ID')) {
      // in my use case, webex webhook failed to initiate
      // your use case might differ
    }
  })
  .on('uncaughtException', (err) => {
    console.error('***** Uncaught Exception thrown', err);
    process.exit(1);
  });

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

process
  .on('unhandledRejection', (reason, promise) => {
    if (reason.message.includes('WEBEX_TRACKING_ID')) {
      const retryAfter = reason.message.trim().slice(-1);
      // retryAfter is given in seconds, setTimeout uses milliseconds
      setTimeout(() => startWebexWebhook(), retryAfter * 1000);
    } else {
      console.error(
        '***** Unhandled Rejection at:',
        promise,
        'reason:',
        reason
      );
    }
  })
  .on('uncaughtException', (err) => {
    console.error('***** Uncaught Exception thrown', err);
    process.exit(1);
  });

Are you seeing unwanted error logs coming from the webex (npm) library?

Override your console error instance:

const consoleErrorInstance = console.error;
console.error = (message, ...rest) => {
    if (message.includes("LoggerProxy->warn#NO LOGGER DEFINED")) {
        return;
    }
    consoleErrorInstance(message, ...rest);
}

@pgoldweic
Copy link

Thanks for suggesting the above @penguinsource, as the same mechanism could be used for other errors too (not related to webex, slack, credentials, etc.) since any uncaught exception will otherwise cause the server to exit).

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

No branches or pull requests