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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NO_DATA_FROM_SOURCE]
  32. = 'The track has stopped receiving data from it\'s source';
  33. // FIXME: Using prototype inheritance because otherwise instanceof is not
  34. // working properly (see https://github.com/babel/babel/issues/3083)
  35. /**
  36. *
  37. * Represents an error that occurred to a JitsiTrack. Can represent various
  38. * types of errors. For error descriptions (@see JitsiTrackErrors).
  39. *
  40. * @extends Error
  41. *
  42. *
  43. * @constructor
  44. * @param {Object|string} error - error object or error name
  45. * @param {Object|string} (options) - getUserMedia constraints object or
  46. * error message
  47. * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
  48. * list of getUserMedia requested devices
  49. */
  50. function JitsiTrackError(error, options, devices) {
  51. if (typeof error === 'object' && typeof error.name !== 'undefined') {
  52. /**
  53. * Additional information about original getUserMedia error
  54. * and constraints.
  55. * @type {{
  56. * error: Object,
  57. * constraints: Object,
  58. * devices: Array.<'audio'|'video'|'desktop'|'screen'>
  59. * }}
  60. */
  61. this.gum = {
  62. error,
  63. constraints: options,
  64. devices: devices && Array.isArray(devices)
  65. ? devices.slice(0)
  66. : undefined
  67. };
  68. switch (error.name) {
  69. case 'NotAllowedError':
  70. case 'PermissionDeniedError':
  71. case 'SecurityError':
  72. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  73. this.message
  74. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  75. + (this.gum.devices || []).join(', ');
  76. break;
  77. case 'DevicesNotFoundError':
  78. case 'NotFoundError':
  79. this.name = JitsiTrackErrors.NOT_FOUND;
  80. this.message
  81. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  82. + (this.gum.devices || []).join(', ');
  83. break;
  84. case 'ConstraintNotSatisfiedError':
  85. case 'OverconstrainedError': {
  86. const constraintName = error.constraintName || error.constraint;
  87. if (options
  88. && options.video
  89. && (!devices || devices.indexOf('video') > -1)
  90. && (constraintName === 'minWidth'
  91. || constraintName === 'maxWidth'
  92. || constraintName === 'minHeight'
  93. || constraintName === 'maxHeight'
  94. || constraintName === 'width'
  95. || constraintName === 'height')) {
  96. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  97. this.message
  98. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  99. + getResolutionFromFailedConstraint(
  100. constraintName,
  101. options);
  102. } else {
  103. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  104. this.message
  105. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  106. + error.constraintName;
  107. }
  108. break;
  109. }
  110. default:
  111. this.name = JitsiTrackErrors.GENERAL;
  112. this.message
  113. = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
  114. break;
  115. }
  116. } else if (typeof error === 'string') {
  117. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  118. this.name = error;
  119. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  120. } else {
  121. // this is some generic error that do not fit any of our
  122. // pre-defined errors, so don't give it any specific name, just
  123. // store message
  124. this.message = error;
  125. }
  126. } else {
  127. throw new Error('Invalid arguments');
  128. }
  129. this.stack = error.stack || (new Error()).stack;
  130. }
  131. JitsiTrackError.prototype = Object.create(Error.prototype);
  132. JitsiTrackError.prototype.constructor = JitsiTrackError;
  133. /**
  134. * Gets failed resolution constraint from corresponding object.
  135. * @param {string} failedConstraintName
  136. * @param {Object} constraints
  137. * @returns {string|number}
  138. */
  139. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  140. if (constraints && constraints.video && constraints.video.mandatory) {
  141. switch (failedConstraintName) {
  142. case 'width':
  143. return constraints.video.mandatory.minWidth;
  144. case 'height':
  145. return constraints.video.mandatory.minHeight;
  146. default:
  147. return constraints.video.mandatory[failedConstraintName] || '';
  148. }
  149. }
  150. return '';
  151. }
  152. export default JitsiTrackError;