twilio-node icon indicating copy to clipboard operation
twilio-node copied to clipboard

Unhandled Rejection in Twilio SDK When Catching Exceptions in Application

Open mimu0 opened this issue 1 year ago • 7 comments

Issue Summary

Twilio SDK has unhandled rejection in SDK and can't catch it from the application. This issue causes application termination.

Steps to Reproduce

  1. See the code snippet

Code Snippet

const client = require('twilio')(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
const getMessages = async () => {
  try {
    const result =  await client.conversations.v1.services('SERVICE_SID').conversations('NOT_EXISTING_CONVERSATION_SID').messages.list();
    return result;
  } catch (e) {
    console.log('------catch from getMessages', e);
  }
};

process.on('unhandledRejection', (reason, promise) => {
  // this should not be called since we catch every exception
  console.log('------unhandledRejection', reason, promise);
});
process.on('rejectionHandled', (promise) => {
  console.log('------rejectionHandled', promise);

});

getMessages().then((messages) => {
  console.log('------messages', messages);
}).catch((e) => {
  console.log('------catch from caller', e);
});

Exception/Log

------catch from getMessages RestException [Error]: The requested resource /Services/ISe0ef118171da46b9bd8ef1d33c82349e/Conversations/CHc8c5b974343a4546bb022e511885b6f1/Messages was not found
    at MessagePage.processResponse (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/base/Page.js:135:19)
    at new Page (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/base/Page.js:17:28)
    at new MessagePage (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/rest/conversations/v1/service/conversation/message.js:315:9)
    at /Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/rest/conversations/v1/service/conversation/message.js:282:63
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 404,
  code: 20404,
  moreInfo: 'https://www.twilio.com/docs/errors/20404',
  details: undefined
}
------messages undefined
------unhandledRejection RestException [Error]: The requested resource /Services/ISe0ef118171da46b9bd8ef1d33c82349e/Conversations/CHc8c5b974343a4546bb022e511885b6f1/Messages was not found
    at MessagePage.processResponse (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/base/Page.js:135:19)
    at new Page (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/base/Page.js:17:28)
    at new MessagePage (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/rest/conversations/v1/service/conversation/message.js:315:9)
    at /Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/rest/conversations/v1/service/conversation/message.js:282:63
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 404,
  code: 20404,
  moreInfo: 'https://www.twilio.com/docs/errors/20404',
  details: undefined
} Promise {
  <rejected> RestException [Error]: The requested resource /Services/ISe0ef118171da46b9bd8ef1d33c82349e/Conversations/CHc8c5b974343a4546bb022e511885b6f1/Messages was not found
      at MessagePage.processResponse (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/base/Page.js:135:19)
      at new Page (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/base/Page.js:17:28)
      at new MessagePage (/Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/rest/conversations/v1/service/conversation/message.js:315:9)
      at /Users/mimu/dev/summer-health/tmp/scripts/node_modules/twilio/lib/rest/conversations/v1/service/conversation/message.js:282:63
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    status: 404,
    code: 20404,
    moreInfo: 'https://www.twilio.com/docs/errors/20404',
    details: undefined
  }
}

Technical details:

  • twilio-node version: 4.14.0
  • node version: 18.14.1

mimu0 avatar Jul 20 '23 02:07 mimu0

We are experiencing this issue as well on the same library and node versions. Please advise - this is causing significant issues in our application.

jbzd avatar Jul 28 '23 22:07 jbzd

I am able to reproduce this issue.

sbansla avatar Aug 01 '23 05:08 sbansla

Internal ticket has been created, we are working actively working on it.

sbansla avatar Aug 02 '23 05:08 sbansla

https://github.com/twilio/twilio-node/blob/main/src/rest/conversations/v1/service/conversation/message.ts#L837 Above code snippet have not handled exception raised from: https://github.com/twilio/twilio-node/blob/main/src/base/Page.ts#L226

--- We are checking our generator code, can this issue occur for all apis.

sbansla avatar Aug 02 '23 11:08 sbansla

I can confirm this also happens for me with other APIs. Saw hundreds of these today, unfortunately:

There was an uncaught error: The requested resource /2010-04-01/Accounts/[redacted]/Calls/[redacted]/Events.json was not found

The code for this is simply:

try {
  const currentCall = await twilioClient.calls(CallSid).fetch();
  const currentCallEvents = await currentCall.events().list();
  // ...trimmed for brevity...
} catch (error) {
  logger.error("Error fetching call events", error);
}

dbburgess avatar Aug 08 '23 20:08 dbburgess

Hi @dbburgess, Fix has been released, let us know if this works for you.

sbansla avatar Aug 10 '23 13:08 sbansla

@sbansla Thank you, I'll upgrade and test it out.

CC: @mimu0 @jbzd

dbburgess avatar Aug 14 '23 14:08 dbburgess

Closing this issue as no feedback was received in last 30 days

tiwarishubham635 avatar Apr 15 '24 09:04 tiwarishubham635