botmasterai / botmaster-messenger

The Facebook Messenger Botmaster integration
MIT License
15 stars 9 forks source link

Not able to capture error in middleware or main botmaster file #16

Open abhishekchotaliya opened 6 years ago

abhishekchotaliya commented 6 years ago

In my application, I am generating access token with the api, which has a lifespan of around 2 hours.

After that access token expires, If I start the botmaster, then it is throwing following error:

Unhandled rejection StatusCodeError: 400 - {"error":{"message":"Error validating access token: The session is invalid because the user logged out.","type":"OAuthException","code":190,"error_subcode":467,"fbtrace_id":"********"}}
    at new StatusCodeError (/Users/abi/code/github/mcbotplatform/api/node_modules/request-promise-core/lib/errors.js:32:15)
    at Request.plumbing.callback (/Users/abi/code/github/mcbotplatform/api/node_modules/request-promise-core/lib/plumbing.js:104:33)
    at Request.RP$callback [as _callback] (/Users/abi/code/github/mcbotplatform/api/node_modules/request-promise-core/lib/plumbing.js:46:31)
    at Request.self.callback (/Users/abi/code/github/mcbotplatform/api/node_modules/request/request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (/Users/abi/code/github/mcbotplatform/api/node_modules/request/request.js:1157:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (/Users/abi/code/github/mcbotplatform/api/node_modules/request/request.js:1079:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

and after that, I am continuously getting this error when anyone sends any messages to my facebook bot(which is linked to botmaster)

Error: wrong signature
    at MessengerBot.__verifyRequestSignature (/Users/abi/code/github/mcbotplatform/api/node_modules/botmaster-messenger/lib/messenger_bot.js:174:13)
    at /Users/abi/code/github/mcbotplatform/api/node_modules/botmaster-messenger/node_modules/body-parser/lib/read.js:104:9
    at invokeCallback (/Users/abi/code/github/mcbotplatform/api/node_modules/botmaster-messenger/node_modules/raw-body/index.js:224:16)
    at done (/Users/abi/code/github/mcbotplatform/api/node_modules/botmaster-messenger/node_modules/raw-body/index.js:213:7)
    at IncomingMessage.onEnd (/Users/abi/code/github/mcbotplatform/api/node_modules/botmaster-messenger/node_modules/raw-body/index.js:273:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

Now, I know how to fix this access token issue. But, the problem is that I am not able to capture this error in my code. Seems like the error message is not coming to my incoming middleware or main botmaster file (where we configure all the channels), and it is getting resolved in the botmaster/botmaster-messenger only.

I've also tried to capture error using these event listeners. but still no luck.

botmaster.on('listening', (message) => {
  console.log(message);
});

botmaster.on('error', (bot, err) => {
  console.log(bot.type);
  console.log(err.stack);
});
jdwuarin commented 6 years ago

That does look like an issue. The first error should be emitted back up so as to be caught by your code:

botmaster.on('error', (bot, err) => {
  console.log(bot.type);
  console.log(err.stack);
});

But it seems like it isn't. If you could write a test to be able to repeatedly get this error, that would allow us to see where (and why) the error isn't getting emitted back up to the botmaster EventEmitter and we should be able to fix this from there. Maybe even something you could find out yourself as you test this.

For some inspiration on how to write tests, check out the tests folder. From there, it should be much easier to fix the issue.

Cheers