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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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')[]} (devices) - list of
  34. * 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 = error.message
  59. || TRACK_ERROR_TO_MESSAGE_MAP[
  60. JitsiTrackErrors.PERMISSION_DENIED]
  61. + (this.gum.devices || []).join(", ");
  62. break;
  63. case "NotFoundError":
  64. this.name = JitsiTrackErrors.NOT_FOUND;
  65. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  66. JitsiTrackErrors.NOT_FOUND]
  67. + (this.gum.devices || []).join(", ");
  68. break;
  69. case "ConstraintNotSatisfiedError":
  70. case "OverconstrainedError":
  71. var constraintName = error.constraintName;
  72. if (options && options.video
  73. && (devices || []).indexOf('video') > -1
  74. &&
  75. (constraintName === "minWidth" ||
  76. constraintName === "maxWidth" ||
  77. constraintName === "minHeight" ||
  78. constraintName === "maxHeight" ||
  79. constraintName === "width" ||
  80. constraintName === "height")) {
  81. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  82. this.message = error.message ||
  83. TRACK_ERROR_TO_MESSAGE_MAP[
  84. JitsiTrackErrors.UNSUPPORTED_RESOLUTION] +
  85. getResolutionFromFailedConstraint(constraintName,
  86. options);
  87. } else {
  88. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  89. this.message = error.message ||
  90. TRACK_ERROR_TO_MESSAGE_MAP[
  91. JitsiTrackErrors.CONSTRAINT_FAILED] +
  92. error.constraintName;
  93. }
  94. break;
  95. default:
  96. this.name = JitsiTrackErrors.GENERAL;
  97. this.message = error.message ||
  98. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL];
  99. break;
  100. }
  101. } else if (typeof error === "string") {
  102. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  103. this.name = error;
  104. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  105. } else {
  106. // this is some generic error that do not fit any of our pre-defined
  107. // errors, so don't give it any specific name, just store message
  108. this.message = error;
  109. }
  110. } else {
  111. throw new Error("Invalid arguments");
  112. }
  113. this.stack = error.stack || (new Error()).stack;
  114. }
  115. JitsiTrackError.prototype = Object.create(Error.prototype);
  116. JitsiTrackError.prototype.constructor = JitsiTrackError;
  117. /**
  118. * Gets failed resolution constraint from corresponding object.
  119. * @param {string} failedConstraintName
  120. * @param {Object} constraints
  121. * @returns {string|number}
  122. */
  123. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  124. if (constraints && constraints.video && constraints.video.mandatory) {
  125. if (failedConstraintName === "width") {
  126. return constraints.video.mandatory.minWidth;
  127. } else if (failedConstraintName === "height") {
  128. return constraints.video.mandatory.minHeight;
  129. } else {
  130. return constraints.video.mandatory[failedConstraintName] || "";
  131. }
  132. }
  133. return "";
  134. }
  135. module.exports = JitsiTrackError;