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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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[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.ELECTRON_DESKTOP_PICKER_ERROR]
  14. = 'Unkown error from desktop picker';
  15. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]
  16. = 'Failed to detect desktop picker';
  17. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL]
  18. = 'Generic getUserMedia error';
  19. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.PERMISSION_DENIED]
  20. = 'User denied permission to use device(s): ';
  21. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NOT_FOUND]
  22. = 'Requested device(s) was/were not found: ';
  23. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CONSTRAINT_FAILED]
  24. = 'Constraint could not be satisfied: ';
  25. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_IS_DISPOSED]
  26. = 'Track has been already disposed';
  27. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_NO_STREAM_FOUND]
  28. = 'Track does not have an associated Media Stream';
  29. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS]
  30. = 'Track mute/unmute process is currently in progress';
  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 'PermissionDeniedError':
  70. case 'SecurityError':
  71. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  72. this.message
  73. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  74. + (this.gum.devices || []).join(', ');
  75. break;
  76. case 'DevicesNotFoundError':
  77. case 'NotFoundError':
  78. this.name = JitsiTrackErrors.NOT_FOUND;
  79. this.message
  80. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  81. + (this.gum.devices || []).join(', ');
  82. break;
  83. case 'ConstraintNotSatisfiedError':
  84. case 'OverconstrainedError': {
  85. const constraintName = error.constraintName;
  86. if (options
  87. && options.video
  88. && (!devices || devices.indexOf('video') > -1)
  89. && (constraintName === 'minWidth'
  90. || constraintName === 'maxWidth'
  91. || constraintName === 'minHeight'
  92. || constraintName === 'maxHeight'
  93. || constraintName === 'width'
  94. || constraintName === 'height')) {
  95. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  96. this.message
  97. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  98. + getResolutionFromFailedConstraint(
  99. constraintName,
  100. options);
  101. } else {
  102. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  103. this.message
  104. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  105. + error.constraintName;
  106. }
  107. break;
  108. }
  109. default:
  110. this.name = JitsiTrackErrors.GENERAL;
  111. this.message
  112. = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
  113. break;
  114. }
  115. } else if (typeof error === 'string') {
  116. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  117. this.name = error;
  118. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  119. } else {
  120. // this is some generic error that do not fit any of our
  121. // pre-defined errors, so don't give it any specific name, just
  122. // store message
  123. this.message = error;
  124. }
  125. } else {
  126. throw new Error('Invalid arguments');
  127. }
  128. this.stack = error.stack || (new Error()).stack;
  129. }
  130. JitsiTrackError.prototype = Object.create(Error.prototype);
  131. JitsiTrackError.prototype.constructor = JitsiTrackError;
  132. /**
  133. * Gets failed resolution constraint from corresponding object.
  134. * @param {string} failedConstraintName
  135. * @param {Object} constraints
  136. * @returns {string|number}
  137. */
  138. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  139. if (constraints && constraints.video && constraints.video.mandatory) {
  140. switch (failedConstraintName) {
  141. case 'width':
  142. return constraints.video.mandatory.minWidth;
  143. case 'height':
  144. return constraints.video.mandatory.minHeight;
  145. default:
  146. return constraints.video.mandatory[failedConstraintName] || '';
  147. }
  148. }
  149. return '';
  150. }
  151. export default JitsiTrackError;