0

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?

  • maybe this could help https://stackoverflow.com/questions/7310521/node-js-best-practice-exception-handling or you can call `callback(true)` on error and handle the error on the other ways – mandaputtra Jan 14 '19 at 03:59
  • Thanks, that kinda helps, if I put it in a domain, then at least it doesn't crash the application.. eg. d.run(function(){ client.video.rooms.create( ... But is that the best solution? – Cormac Guerin Jan 14 '19 at 07:36

1 Answers1

5

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();
};
Sridhar
  • 11,466
  • 5
  • 39
  • 43