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

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