I want to know if an error in the following promises chain has been generated because of the getUserMedia() function or because of the webRTC functions. Firefox documentation says: Rejections of the returned promise are made by passing a DOMException error object to the promise's failure handler. So, how can I know if the error object that I get is a DOMException error object, in both chrome and firefox?
function handleVideoOfferMsg(msg) {
var localStream = null;
targetUsername = msg.name;
createPeerConnection();
var desc = new RTCSessionDescription(msg.sdp);
myPeerConnection.setRemoteDescription(desc).then(function () {
return navigator.mediaDevices.getUserMedia(mediaConstraints);
})
.then(function(stream) {
localStream = stream;
document.getElementById("local_video").srcObject = localStream;
localStream.getTracks().forEach(track => myPeerConnection.addTrack(track, localStream));
})
.then(function() {
return myPeerConnection.createAnswer();
})
.then(function(answer) {
return myPeerConnection.setLocalDescription(answer);
})
.then(function() {
var msg = {
name: myUsername,
target: targetUsername,
type: "video-answer",
sdp: myPeerConnection.localDescription
};
sendToServer(msg);
})
.catch(function(e){
//know if error is generated by getUserMedia()
});
}
The right way to do this would not be to look at the error object, but to catch the error at the right point in the flow.
e.g you could have
.then(function(stream) { ...},
(err) => { gumErrored = true; throw(err) ; }
)
That way, you would still be able to deal with it the final catch (assuming that's what you want) by checking if the gumErrored
flag was true, but with certainty about the origin of the error.
(you could skip one promise in the chain by bringing return myPeerConnection.createAnswer()
into that same block)