twilio / twilio-node

Node.js helper library
MIT License
1.37k stars 495 forks source link

Unhandled Rejection in Twilio SDK When Catching Exceptions in Application #949

Closed mimu0 closed 2 months ago

mimu0 commented 11 months ago

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:

jbzd commented 11 months ago

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

sbansla commented 11 months ago

I am able to reproduce this issue.

sbansla commented 11 months ago

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

sbansla commented 11 months ago

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.

dbburgess commented 11 months ago

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);
}
sbansla commented 11 months ago

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

dbburgess commented 10 months ago

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

CC: @mimu0 @jbzd

tiwarishubham635 commented 2 months ago

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