errorUtils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Utility functions for CSSOM error handling
  2. /**
  3. * Gets the appropriate error constructor from the global object context.
  4. * Tries to find the error constructor from parentStyleSheet.__globalObject,
  5. * then from __globalObject, then falls back to the native constructor.
  6. *
  7. * @param {Object} context - The CSSOM object (rule, stylesheet, etc.)
  8. * @param {string} errorType - The error type ('TypeError', 'RangeError', 'DOMException', etc.)
  9. * @return {Function} The error constructor
  10. */
  11. function getErrorConstructor(context, errorType) {
  12. // Try parentStyleSheet.__globalObject first
  13. if (context.parentStyleSheet && context.parentStyleSheet.__globalObject && context.parentStyleSheet.__globalObject[errorType]) {
  14. return context.parentStyleSheet.__globalObject[errorType];
  15. }
  16. // Try __parentStyleSheet (alternative naming)
  17. if (context.__parentStyleSheet && context.__parentStyleSheet.__globalObject && context.__parentStyleSheet.__globalObject[errorType]) {
  18. return context.__parentStyleSheet.__globalObject[errorType];
  19. }
  20. // Try __globalObject on the context itself
  21. if (context.__globalObject && context.__globalObject[errorType]) {
  22. return context.__globalObject[errorType];
  23. }
  24. // Fall back to native constructor
  25. return (typeof global !== 'undefined' && global[errorType]) ||
  26. (typeof window !== 'undefined' && window[errorType]) ||
  27. eval(errorType);
  28. }
  29. /**
  30. * Creates an appropriate error with context-aware constructor.
  31. *
  32. * @param {Object} context - The CSSOM object (rule, stylesheet, etc.)
  33. * @param {string} errorType - The error type ('TypeError', 'RangeError', 'DOMException', etc.)
  34. * @param {string} message - The error message
  35. * @param {string} [name] - Optional name for DOMException
  36. */
  37. function createError(context, errorType, message, name) {
  38. var ErrorConstructor = getErrorConstructor(context, errorType);
  39. return new ErrorConstructor(message, name);
  40. }
  41. /**
  42. * Creates and throws an appropriate error with context-aware constructor.
  43. *
  44. * @param {Object} context - The CSSOM object (rule, stylesheet, etc.)
  45. * @param {string} errorType - The error type ('TypeError', 'RangeError', 'DOMException', etc.)
  46. * @param {string} message - The error message
  47. * @param {string} [name] - Optional name for DOMException
  48. */
  49. function throwError(context, errorType, message, name) {
  50. throw createError(context, errorType, message, name);
  51. }
  52. /**
  53. * Throws a TypeError for missing required arguments.
  54. *
  55. * @param {Object} context - The CSSOM object
  56. * @param {string} methodName - The method name (e.g., 'appendRule')
  57. * @param {string} objectName - The object name (e.g., 'CSSKeyframesRule')
  58. * @param {number} [required=1] - Number of required arguments
  59. * @param {number} [provided=0] - Number of provided arguments
  60. */
  61. function throwMissingArguments(context, methodName, objectName, required, provided) {
  62. required = required || 1;
  63. provided = provided || 0;
  64. var message = "Failed to execute '" + methodName + "' on '" + objectName + "': " +
  65. required + " argument" + (required > 1 ? "s" : "") + " required, but only " +
  66. provided + " present.";
  67. throwError(context, 'TypeError', message);
  68. }
  69. /**
  70. * Throws a DOMException for parse errors.
  71. *
  72. * @param {Object} context - The CSSOM object
  73. * @param {string} methodName - The method name
  74. * @param {string} objectName - The object name
  75. * @param {string} rule - The rule that failed to parse
  76. * @param {string} [name='SyntaxError'] - The DOMException name
  77. */
  78. function throwParseError(context, methodName, objectName, rule, name) {
  79. var message = "Failed to execute '" + methodName + "' on '" + objectName + "': " +
  80. "Failed to parse the rule '" + rule + "'.";
  81. throwError(context, 'DOMException', message, name || 'SyntaxError');
  82. }
  83. /**
  84. * Throws a DOMException for index errors.
  85. *
  86. * @param {Object} context - The CSSOM object
  87. * @param {string} methodName - The method name
  88. * @param {string} objectName - The object name
  89. * @param {number} index - The invalid index
  90. * @param {number} maxIndex - The maximum valid index
  91. * @param {string} [name='IndexSizeError'] - The DOMException name
  92. */
  93. function throwIndexError(context, methodName, objectName, index, maxIndex, name) {
  94. var message = "Failed to execute '" + methodName + "' on '" + objectName + "': " +
  95. "The index provided (" + index + ") is larger than the maximum index (" + maxIndex + ").";
  96. throwError(context, 'DOMException', message, name || 'IndexSizeError');
  97. }
  98. var errorUtils = {
  99. createError: createError,
  100. getErrorConstructor: getErrorConstructor,
  101. throwError: throwError,
  102. throwMissingArguments: throwMissingArguments,
  103. throwParseError: throwParseError,
  104. throwIndexError: throwIndexError
  105. };
  106. //.CommonJS
  107. exports.errorUtils = errorUtils;
  108. ///CommonJS