errors.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. 'use strict';
  2. var conventions = require('./conventions');
  3. function extendError(constructor, writableName) {
  4. constructor.prototype = Object.create(Error.prototype, {
  5. constructor: { value: constructor },
  6. name: { value: constructor.name, enumerable: true, writable: writableName },
  7. });
  8. }
  9. var DOMExceptionName = conventions.freeze({
  10. /**
  11. * the default value as defined by the spec
  12. */
  13. Error: 'Error',
  14. /**
  15. * @deprecated
  16. * Use RangeError instead.
  17. */
  18. IndexSizeError: 'IndexSizeError',
  19. /**
  20. * @deprecated
  21. * Just to match the related static code, not part of the spec.
  22. */
  23. DomstringSizeError: 'DomstringSizeError',
  24. HierarchyRequestError: 'HierarchyRequestError',
  25. WrongDocumentError: 'WrongDocumentError',
  26. InvalidCharacterError: 'InvalidCharacterError',
  27. /**
  28. * @deprecated
  29. * Just to match the related static code, not part of the spec.
  30. */
  31. NoDataAllowedError: 'NoDataAllowedError',
  32. NoModificationAllowedError: 'NoModificationAllowedError',
  33. NotFoundError: 'NotFoundError',
  34. NotSupportedError: 'NotSupportedError',
  35. InUseAttributeError: 'InUseAttributeError',
  36. InvalidStateError: 'InvalidStateError',
  37. SyntaxError: 'SyntaxError',
  38. InvalidModificationError: 'InvalidModificationError',
  39. NamespaceError: 'NamespaceError',
  40. /**
  41. * @deprecated
  42. * Use TypeError for invalid arguments,
  43. * "NotSupportedError" DOMException for unsupported operations,
  44. * and "NotAllowedError" DOMException for denied requests instead.
  45. */
  46. InvalidAccessError: 'InvalidAccessError',
  47. /**
  48. * @deprecated
  49. * Just to match the related static code, not part of the spec.
  50. */
  51. ValidationError: 'ValidationError',
  52. /**
  53. * @deprecated
  54. * Use TypeError instead.
  55. */
  56. TypeMismatchError: 'TypeMismatchError',
  57. SecurityError: 'SecurityError',
  58. NetworkError: 'NetworkError',
  59. AbortError: 'AbortError',
  60. /**
  61. * @deprecated
  62. * Just to match the related static code, not part of the spec.
  63. */
  64. URLMismatchError: 'URLMismatchError',
  65. QuotaExceededError: 'QuotaExceededError',
  66. TimeoutError: 'TimeoutError',
  67. InvalidNodeTypeError: 'InvalidNodeTypeError',
  68. DataCloneError: 'DataCloneError',
  69. EncodingError: 'EncodingError',
  70. NotReadableError: 'NotReadableError',
  71. UnknownError: 'UnknownError',
  72. ConstraintError: 'ConstraintError',
  73. DataError: 'DataError',
  74. TransactionInactiveError: 'TransactionInactiveError',
  75. ReadOnlyError: 'ReadOnlyError',
  76. VersionError: 'VersionError',
  77. OperationError: 'OperationError',
  78. NotAllowedError: 'NotAllowedError',
  79. OptOutError: 'OptOutError',
  80. });
  81. var DOMExceptionNames = Object.keys(DOMExceptionName);
  82. function isValidDomExceptionCode(value) {
  83. return typeof value === 'number' && value >= 1 && value <= 25;
  84. }
  85. function endsWithError(value) {
  86. return typeof value === 'string' && value.substring(value.length - DOMExceptionName.Error.length) === DOMExceptionName.Error;
  87. }
  88. /**
  89. * DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation
  90. * is impossible to perform (either for logical reasons, because data is lost, or because the
  91. * implementation has become unstable). In general, DOM methods return specific error values in
  92. * ordinary processing situations, such as out-of-bound errors when using NodeList.
  93. *
  94. * Implementations should raise other exceptions under other circumstances. For example,
  95. * implementations should raise an implementation-dependent exception if a null argument is
  96. * passed when null was not expected.
  97. *
  98. * This implementation supports the following usages:
  99. * 1. according to the living standard (both arguments are optional):
  100. * ```
  101. * new DOMException("message (can be empty)", DOMExceptionNames.HierarchyRequestError)
  102. * ```
  103. * 2. according to previous xmldom implementation (only the first argument is required):
  104. * ```
  105. * new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "optional message")
  106. * ```
  107. * both result in the proper name being set.
  108. *
  109. * @class DOMException
  110. * @param {number | string} messageOrCode
  111. * The reason why an operation is not acceptable.
  112. * If it is a number, it is used to determine the `name`, see
  113. * {@link https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-258A00AF ExceptionCode}
  114. * @param {string | keyof typeof DOMExceptionName | Error} [nameOrMessage]
  115. * The `name` to use for the error.
  116. * If `messageOrCode` is a number, this arguments is used as the `message` instead.
  117. * @augments Error
  118. * @see https://webidl.spec.whatwg.org/#idl-DOMException
  119. * @see https://webidl.spec.whatwg.org/#dfn-error-names-table
  120. * @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-17189187
  121. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
  122. * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
  123. */
  124. function DOMException(messageOrCode, nameOrMessage) {
  125. // support old way of passing arguments: first argument is a valid number
  126. if (isValidDomExceptionCode(messageOrCode)) {
  127. this.name = DOMExceptionNames[messageOrCode];
  128. this.message = nameOrMessage || '';
  129. } else {
  130. this.message = messageOrCode;
  131. this.name = endsWithError(nameOrMessage) ? nameOrMessage : DOMExceptionName.Error;
  132. }
  133. if (Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
  134. }
  135. extendError(DOMException, true);
  136. Object.defineProperties(DOMException.prototype, {
  137. code: {
  138. enumerable: true,
  139. get: function () {
  140. var code = DOMExceptionNames.indexOf(this.name);
  141. if (isValidDomExceptionCode(code)) return code;
  142. return 0;
  143. },
  144. },
  145. });
  146. var ExceptionCode = {
  147. INDEX_SIZE_ERR: 1,
  148. DOMSTRING_SIZE_ERR: 2,
  149. HIERARCHY_REQUEST_ERR: 3,
  150. WRONG_DOCUMENT_ERR: 4,
  151. INVALID_CHARACTER_ERR: 5,
  152. NO_DATA_ALLOWED_ERR: 6,
  153. NO_MODIFICATION_ALLOWED_ERR: 7,
  154. NOT_FOUND_ERR: 8,
  155. NOT_SUPPORTED_ERR: 9,
  156. INUSE_ATTRIBUTE_ERR: 10,
  157. INVALID_STATE_ERR: 11,
  158. SYNTAX_ERR: 12,
  159. INVALID_MODIFICATION_ERR: 13,
  160. NAMESPACE_ERR: 14,
  161. INVALID_ACCESS_ERR: 15,
  162. VALIDATION_ERR: 16,
  163. TYPE_MISMATCH_ERR: 17,
  164. SECURITY_ERR: 18,
  165. NETWORK_ERR: 19,
  166. ABORT_ERR: 20,
  167. URL_MISMATCH_ERR: 21,
  168. QUOTA_EXCEEDED_ERR: 22,
  169. TIMEOUT_ERR: 23,
  170. INVALID_NODE_TYPE_ERR: 24,
  171. DATA_CLONE_ERR: 25,
  172. };
  173. var entries = Object.entries(ExceptionCode);
  174. for (var i = 0; i < entries.length; i++) {
  175. var key = entries[i][0];
  176. DOMException[key] = entries[i][1];
  177. }
  178. /**
  179. * Creates an error that will not be caught by XMLReader aka the SAX parser.
  180. *
  181. * @class
  182. * @param {string} message
  183. * @param {any} [locator]
  184. */
  185. function ParseError(message, locator) {
  186. this.message = message;
  187. this.locator = locator;
  188. if (Error.captureStackTrace) Error.captureStackTrace(this, ParseError);
  189. }
  190. extendError(ParseError);
  191. exports.DOMException = DOMException;
  192. exports.DOMExceptionName = DOMExceptionName;
  193. exports.ExceptionCode = ExceptionCode;
  194. exports.ParseError = ParseError;