media-error.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _obj = require('./utils/obj');
  4. /**
  5. * A Custom `MediaError` class which mimics the standard HTML5 `MediaError` class.
  6. *
  7. * @param {number|string|Object|MediaError} value
  8. * This can be of multiple types:
  9. * - number: should be a standard error code
  10. * - string: an error message (the code will be 0)
  11. * - Object: arbitrary properties
  12. * - `MediaError` (native): used to populate a video.js `MediaError` object
  13. * - `MediaError` (video.js): will return itself if it's already a
  14. * video.js `MediaError` object.
  15. *
  16. * @see [MediaError Spec]{@link https://dev.w3.org/html5/spec-author-view/video.html#mediaerror}
  17. * @see [Encrypted MediaError Spec]{@link https://www.w3.org/TR/2013/WD-encrypted-media-20130510/#error-codes}
  18. *
  19. * @class MediaError
  20. */
  21. function MediaError(value) {
  22. // Allow redundant calls to this constructor to avoid having `instanceof`
  23. // checks peppered around the code.
  24. if (value instanceof MediaError) {
  25. return value;
  26. }
  27. if (typeof value === 'number') {
  28. this.code = value;
  29. } else if (typeof value === 'string') {
  30. // default code is zero, so this is a custom error
  31. this.message = value;
  32. } else if ((0, _obj.isObject)(value)) {
  33. // We assign the `code` property manually because native `MediaError` objects
  34. // do not expose it as an own/enumerable property of the object.
  35. if (typeof value.code === 'number') {
  36. this.code = value.code;
  37. }
  38. (0, _obj.assign)(this, value);
  39. }
  40. if (!this.message) {
  41. this.message = MediaError.defaultMessages[this.code] || '';
  42. }
  43. }
  44. /**
  45. * The error code that refers two one of the defined `MediaError` types
  46. *
  47. * @type {Number}
  48. */
  49. /**
  50. * @file media-error.js
  51. */
  52. MediaError.prototype.code = 0;
  53. /**
  54. * An optional message that to show with the error. Message is not part of the HTML5
  55. * video spec but allows for more informative custom errors.
  56. *
  57. * @type {String}
  58. */
  59. MediaError.prototype.message = '';
  60. /**
  61. * An optional status code that can be set by plugins to allow even more detail about
  62. * the error. For example a plugin might provide a specific HTTP status code and an
  63. * error message for that code. Then when the plugin gets that error this class will
  64. * know how to display an error message for it. This allows a custom message to show
  65. * up on the `Player` error overlay.
  66. *
  67. * @type {Array}
  68. */
  69. MediaError.prototype.status = null;
  70. /**
  71. * Errors indexed by the W3C standard. The order **CANNOT CHANGE**! See the
  72. * specification listed under {@link MediaError} for more information.
  73. *
  74. * @enum {array}
  75. * @readonly
  76. * @property {string} 0 - MEDIA_ERR_CUSTOM
  77. * @property {string} 1 - MEDIA_ERR_CUSTOM
  78. * @property {string} 2 - MEDIA_ERR_ABORTED
  79. * @property {string} 3 - MEDIA_ERR_NETWORK
  80. * @property {string} 4 - MEDIA_ERR_SRC_NOT_SUPPORTED
  81. * @property {string} 5 - MEDIA_ERR_ENCRYPTED
  82. */
  83. MediaError.errorTypes = ['MEDIA_ERR_CUSTOM', 'MEDIA_ERR_ABORTED', 'MEDIA_ERR_NETWORK', 'MEDIA_ERR_DECODE', 'MEDIA_ERR_SRC_NOT_SUPPORTED', 'MEDIA_ERR_ENCRYPTED'];
  84. /**
  85. * The default `MediaError` messages based on the {@link MediaError.errorTypes}.
  86. *
  87. * @type {Array}
  88. * @constant
  89. */
  90. MediaError.defaultMessages = {
  91. 1: 'You aborted the media playback',
  92. 2: 'A network error caused the media download to fail part-way.',
  93. 3: 'The media playback was aborted due to a corruption problem or because the media used features your browser did not support.',
  94. 4: 'The media could not be loaded, either because the server or network failed or because the format is not supported.',
  95. 5: 'The media is encrypted and we do not have the keys to decrypt it.'
  96. };
  97. // Add types as properties on MediaError
  98. // e.g. MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED = 4;
  99. for (var errNum = 0; errNum < MediaError.errorTypes.length; errNum++) {
  100. MediaError[MediaError.errorTypes[errNum]] = errNum;
  101. // values should be accessible on both the class and instance
  102. MediaError.prototype[MediaError.errorTypes[errNum]] = errNum;
  103. }
  104. // jsdocs for instance/static members added above
  105. // instance methods use `#` and static methods use `.`
  106. /**
  107. * W3C error code for any custom error.
  108. *
  109. * @member MediaError#MEDIA_ERR_CUSTOM
  110. * @constant {number}
  111. * @default 0
  112. */
  113. /**
  114. * W3C error code for any custom error.
  115. *
  116. * @member MediaError.MEDIA_ERR_CUSTOM
  117. * @constant {number}
  118. * @default 0
  119. */
  120. /**
  121. * W3C error code for media error aborted.
  122. *
  123. * @member MediaError#MEDIA_ERR_ABORTED
  124. * @constant {number}
  125. * @default 1
  126. */
  127. /**
  128. * W3C error code for media error aborted.
  129. *
  130. * @member MediaError.MEDIA_ERR_ABORTED
  131. * @constant {number}
  132. * @default 1
  133. */
  134. /**
  135. * W3C error code for any network error.
  136. *
  137. * @member MediaError#MEDIA_ERR_NETWORK
  138. * @constant {number}
  139. * @default 2
  140. */
  141. /**
  142. * W3C error code for any network error.
  143. *
  144. * @member MediaError.MEDIA_ERR_NETWORK
  145. * @constant {number}
  146. * @default 2
  147. */
  148. /**
  149. * W3C error code for any decoding error.
  150. *
  151. * @member MediaError#MEDIA_ERR_DECODE
  152. * @constant {number}
  153. * @default 3
  154. */
  155. /**
  156. * W3C error code for any decoding error.
  157. *
  158. * @member MediaError.MEDIA_ERR_DECODE
  159. * @constant {number}
  160. * @default 3
  161. */
  162. /**
  163. * W3C error code for any time that a source is not supported.
  164. *
  165. * @member MediaError#MEDIA_ERR_SRC_NOT_SUPPORTED
  166. * @constant {number}
  167. * @default 4
  168. */
  169. /**
  170. * W3C error code for any time that a source is not supported.
  171. *
  172. * @member MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED
  173. * @constant {number}
  174. * @default 4
  175. */
  176. /**
  177. * W3C error code for any time that a source is encrypted.
  178. *
  179. * @member MediaError#MEDIA_ERR_ENCRYPTED
  180. * @constant {number}
  181. * @default 5
  182. */
  183. /**
  184. * W3C error code for any time that a source is encrypted.
  185. *
  186. * @member MediaError.MEDIA_ERR_ENCRYPTED
  187. * @constant {number}
  188. * @default 5
  189. */
  190. exports['default'] = MediaError;