简体   繁体   中英

How to handle twilio Errors that crash nodejs

Using the twilio nodejs api crashes node when an error is thrown. For example if the room already exists..

/home/web/node_modules/twilio/node_modules/q/q.js:876 throw error; Error: Room exists

Try catch does not help. Tried wrapping it in a promise, no luck. Tried to first fetch the room, but it crashes in the same way if the room does not exist. Tried to retrieve a list of all in-progress rooms, but it stalls forever. I can get a list of all completed rooms but I need to check in-progress ones. Either way crashing node is no good, need to be able to handle these eventualities.

  exports.createBackendRoom = function (roomname, callback) { try { client.video.rooms.create({uniqueName: roomname}) .then(function(room) { console.log(room); callback(true); }).done(); } catch(e) { console.log(e); callback(false); } } 

Unable to handle the errors..

/home/web/node_modules/twilio/node_modules/q/q.js:876 throw error; Error: Room exists

How can I gracefully handle these?

Try catch does not help.

This is because error is being thrown by asynchronous operation. Try-catch will handle the error thrown by synchronous operation. To handle asynchronous errors, add a catch(err=>{}) block

exports.createBackendRoom = function (roomname, callback) {
  client.video.rooms.create({uniqueName: roomname})
    .then(function (room) {
      console.log(room);
      callback(true);
    }).catch(err => {
    console.log(err); // handle error
    callback(false);
  }).done();
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM