In my twillio application i’m getting random errors when tryin to disconnect from the room.
This is the code I’m trying to execute upon exit:
this.log('Leaving room...');
try {
this.activeRoom ? this.activeRoom.disconnect().catch(console.log) : console.log('No active room to disconnect / check disconnect logic');
} catch (error) {
console.log(error)
}
this creates the following error:
I’m not so concerned about the error itself but the fact that i cannot catch the error.
As you can see both try/catch block and .catch() were added but I’m still getting this error.
UPDATE_1
After digging thru the api I found and implemented this:
this.activeRoom.on('disconnected', function(room, error) {
if (error) {
console.log('Unexpectedly disconnected:', error);
}
myRoom.localParticipant.tracks.forEach(function(track) {
track.stop();
track.detach();
});
});
however this error still comes up for me.
UPDATE_2
After implementing solution proposed in an answer this is what I got(still the same error):
3
Answers
Twilio developer evangelist here.
That is weird that you can’t seem to catch the error. I’m not sure I have an answer for that.
However, you could check whether the local participant is connected before trying to disconnect them. A
Participant
object has astate
property that can be “connected”, “disconnected” or “failed”.So, you could:
For anyone that runs into this issue. The stack trace you get from
room.disconnect()
is misleading.The actual error is in your
room.localParticipant.publishTrack(track)
call. That function returns a promise, and will resolve (with an exception) the moment you callroom.disconnect()
.If you haven’t handled failure there, you’ll get the error shown in this question. So just add a
.catch()
statement to all yourpublishTrack
calls.I use this to solve this issue