You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiTrackError.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var JitsiTrackErrors = require("./JitsiTrackErrors");
  2. var TRACK_ERROR_TO_MESSAGE_MAP = {};
  3. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.UNSUPPORTED_RESOLUTION]
  4. = "Video resolution is not supported: ";
  5. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED]
  6. = "Firefox extension is not installed";
  7. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR]
  8. = "Failed to install Chrome extension";
  9. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED]
  10. = "User canceled Chrome's screen sharing prompt";
  11. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR]
  12. = "Unknown error from Chrome extension";
  13. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL]
  14. = "Generic getUserMedia error";
  15. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.PERMISSION_DENIED]
  16. = "User denied permission to use device(s): ";
  17. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NOT_FOUND]
  18. = "Requested device(s) was/were not found: ";
  19. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CONSTRAINT_FAILED]
  20. = "Constraint could not be satisfied: ";
  21. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_IS_DISPOSED]
  22. = "Track has been already disposed";
  23. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS]
  24. = "Track mute/unmute process is currently in progress";
  25. /**
  26. * Object representing error that happened to a JitsiTrack. Can represent
  27. * various types of errors. For error descriptions (@see JitsiTrackErrors).
  28. * @constructor
  29. * @extends Error
  30. * @param {Object|string} error - error object or error name
  31. * @param {Object|string} (options) - getUserMedia constraints object or error
  32. * message
  33. * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
  34. * list of getUserMedia requested devices
  35. */
  36. function JitsiTrackError(error, options, devices) {
  37. if (typeof error === "object" && typeof error.name !== "undefined") {
  38. /**
  39. * Additional information about original getUserMedia error
  40. * and constraints.
  41. * @type {{
  42. * error: Object,
  43. * constraints: Object,
  44. * devices: Array.<'audio'|'video'|'desktop'|'screen'>
  45. * }}
  46. */
  47. this.gum = {
  48. error: error,
  49. constraints: options,
  50. devices: devices && Array.isArray(devices)
  51. ? devices.slice(0)
  52. : undefined
  53. };
  54. switch (error.name) {
  55. case "PermissionDeniedError":
  56. case "SecurityError":
  57. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  58. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  59. JitsiTrackErrors.PERMISSION_DENIED]
  60. + (this.gum.devices || []).join(", ");
  61. break;
  62. case "NotFoundError":
  63. this.name = JitsiTrackErrors.NOT_FOUND;
  64. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  65. JitsiTrackErrors.NOT_FOUND]
  66. + (this.gum.devices || []).join(", ");
  67. break;
  68. case "ConstraintNotSatisfiedError":
  69. case "OverconstrainedError":
  70. var constraintName = error.constraintName;
  71. if (options && options.video
  72. && (devices || []).indexOf('video') > -1
  73. &&
  74. (constraintName === "minWidth" ||
  75. constraintName === "maxWidth" ||
  76. constraintName === "minHeight" ||
  77. constraintName === "maxHeight" ||
  78. constraintName === "width" ||
  79. constraintName === "height")) {
  80. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  81. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  82. JitsiTrackErrors.UNSUPPORTED_RESOLUTION] +
  83. getResolutionFromFailedConstraint(constraintName,
  84. options);
  85. } else {
  86. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  87. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  88. JitsiTrackErrors.CONSTRAINT_FAILED] +
  89. error.constraintName;
  90. }
  91. break;
  92. default:
  93. this.name = JitsiTrackErrors.GENERAL;
  94. this.message = error.message ||
  95. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL];
  96. break;
  97. }
  98. } else if (typeof error === "string") {
  99. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  100. this.name = error;
  101. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  102. } else {
  103. // this is some generic error that do not fit any of our pre-defined
  104. // errors, so don't give it any specific name, just store message
  105. this.message = error;
  106. }
  107. } else {
  108. throw new Error("Invalid arguments");
  109. }
  110. this.stack = error.stack || (new Error()).stack;
  111. }
  112. JitsiTrackError.prototype = Object.create(Error.prototype);
  113. JitsiTrackError.prototype.constructor = JitsiTrackError;
  114. /**
  115. * Gets failed resolution constraint from corresponding object.
  116. * @param {string} failedConstraintName
  117. * @param {Object} constraints
  118. * @returns {string|number}
  119. */
  120. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  121. if (constraints && constraints.video && constraints.video.mandatory) {
  122. if (failedConstraintName === "width") {
  123. return constraints.video.mandatory.minWidth;
  124. } else if (failedConstraintName === "height") {
  125. return constraints.video.mandatory.minHeight;
  126. } else {
  127. return constraints.video.mandatory[failedConstraintName] || "";
  128. }
  129. }
  130. return "";
  131. }
  132. module.exports = JitsiTrackError;