0
votes

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?

1
maybe this could help stackoverflow.com/questions/7310521/… or you can call callback(true) on error and handle the error on the other waysmandaputtra
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

1 Answers

1
votes

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();
};