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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import * as JitsiTrackErrors from './JitsiTrackErrors';
  2. const 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.CHROME_EXTENSION_INSTALLATION_ERROR]
  6. = 'Failed to install Chrome extension';
  7. TRACK_ERROR_TO_MESSAGE_MAP[
  8. JitsiTrackErrors.CHROME_EXTENSION_USER_GESTURE_REQUIRED]
  9. = 'Failed to install Chrome extension - installations can only be initiated'
  10. + ' by a user gesture.';
  11. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED]
  12. = 'User canceled Chrome\'s screen sharing prompt';
  13. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR]
  14. = 'Unknown error from Chrome extension';
  15. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR]
  16. = 'Unkown error from desktop picker';
  17. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]
  18. = 'Failed to detect desktop picker';
  19. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL]
  20. = 'Generic getUserMedia error';
  21. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.PERMISSION_DENIED]
  22. = 'User denied permission to use device(s): ';
  23. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NOT_FOUND]
  24. = 'Requested device(s) was/were not found: ';
  25. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CONSTRAINT_FAILED]
  26. = 'Constraint could not be satisfied: ';
  27. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_IS_DISPOSED]
  28. = 'Track has been already disposed';
  29. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_NO_STREAM_FOUND]
  30. = 'Track does not have an associated Media Stream';
  31. // FIXME: Using prototype inheritance because otherwise instanceof is not
  32. // working properly (see https://github.com/babel/babel/issues/3083)
  33. /**
  34. *
  35. * Represents an error that occurred to a JitsiTrack. Can represent various
  36. * types of errors. For error descriptions (@see JitsiTrackErrors).
  37. *
  38. * @extends Error
  39. *
  40. *
  41. * @constructor
  42. * @param {Object|string} error - error object or error name
  43. * @param {Object|string} (options) - getUserMedia constraints object or
  44. * error message
  45. * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
  46. * list of getUserMedia requested devices
  47. */
  48. function JitsiTrackError(error, options, devices) {
  49. if (typeof error === 'object' && typeof error.name !== 'undefined') {
  50. /**
  51. * Additional information about original getUserMedia error
  52. * and constraints.
  53. * @type {{
  54. * error: Object,
  55. * constraints: Object,
  56. * devices: Array.<'audio'|'video'|'desktop'|'screen'>
  57. * }}
  58. */
  59. this.gum = {
  60. error,
  61. constraints: options,
  62. devices: devices && Array.isArray(devices)
  63. ? devices.slice(0)
  64. : undefined
  65. };
  66. switch (error.name) {
  67. case 'NotAllowedError':
  68. case 'PermissionDeniedError':
  69. case 'SecurityError':
  70. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  71. this.message
  72. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  73. + (this.gum.devices || []).join(', ');
  74. break;
  75. case 'DevicesNotFoundError':
  76. case 'NotFoundError':
  77. this.name = JitsiTrackErrors.NOT_FOUND;
  78. this.message
  79. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  80. + (this.gum.devices || []).join(', ');
  81. break;
  82. case 'ConstraintNotSatisfiedError':
  83. case 'OverconstrainedError': {
  84. const constraintName = error.constraintName || error.constraint;
  85. // we treat deviceId as unsupported resolution, as we want to
  86. // retry and finally if everything fails to remove deviceId from
  87. // mandatory constraints
  88. if (options
  89. && options.video
  90. && (!devices || devices.indexOf('video') > -1)
  91. && (constraintName === 'minWidth'
  92. || constraintName === 'maxWidth'
  93. || constraintName === 'minHeight'
  94. || constraintName === 'maxHeight'
  95. || constraintName === 'width'
  96. || constraintName === 'height'
  97. || constraintName === 'deviceId')) {
  98. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  99. this.message
  100. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  101. + getResolutionFromFailedConstraint(
  102. constraintName,
  103. options);
  104. } else {
  105. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  106. this.message
  107. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  108. + error.constraintName;
  109. }
  110. break;
  111. }
  112. default:
  113. this.name = JitsiTrackErrors.GENERAL;
  114. this.message
  115. = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
  116. break;
  117. }
  118. } else if (typeof error === 'string') {
  119. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  120. this.name = error;
  121. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  122. } else {
  123. // this is some generic error that do not fit any of our
  124. // pre-defined errors, so don't give it any specific name, just
  125. // store message
  126. this.message = error;
  127. }
  128. } else {
  129. throw new Error('Invalid arguments');
  130. }
  131. this.stack = error.stack || (new Error()).stack;
  132. }
  133. JitsiTrackError.prototype = Object.create(Error.prototype);
  134. JitsiTrackError.prototype.constructor = JitsiTrackError;
  135. /**
  136. * Gets failed resolution constraint from corresponding object.
  137. * @param {string} failedConstraintName
  138. * @param {Object} constraints
  139. * @returns {string|number}
  140. */
  141. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  142. if (constraints && constraints.video && constraints.video.mandatory) {
  143. switch (failedConstraintName) {
  144. case 'width':
  145. return constraints.video.mandatory.minWidth;
  146. case 'height':
  147. return constraints.video.mandatory.minHeight;
  148. default:
  149. return constraints.video.mandatory[failedConstraintName] || '';
  150. }
  151. }
  152. return '';
  153. }
  154. export default JitsiTrackError;