DOMException.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/NonnullRefPtr.h>
  29. #include <AK/RefCounted.h>
  30. #include <LibWeb/Bindings/WindowObject.h>
  31. #include <LibWeb/Bindings/Wrappable.h>
  32. namespace Web::DOM {
  33. // The following have a legacy code value but *don't* produce it as
  34. // DOMException.code value when used as name (and are therefore omitted here):
  35. // - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)
  36. // - NoDataAllowedError (NO_DATA_ALLOWED_ERR = 6)
  37. // - ValidationError (VALIDATION_ERR = 16)
  38. #define ENUMERATE_DOM_EXCEPTION_LEGACY_CODES \
  39. __ENUMERATE(IndexSizeError, 1) \
  40. __ENUMERATE(HierarchyRequestError, 3) \
  41. __ENUMERATE(WrongDocumentError, 4) \
  42. __ENUMERATE(InvalidCharacterError, 5) \
  43. __ENUMERATE(NoModificationAllowedError, 7) \
  44. __ENUMERATE(NotFoundError, 8) \
  45. __ENUMERATE(NotSupportedError, 9) \
  46. __ENUMERATE(InUseAttributeError, 10) \
  47. __ENUMERATE(InvalidStateError, 11) \
  48. __ENUMERATE(SyntaxError, 12) \
  49. __ENUMERATE(InvalidModificationError, 13) \
  50. __ENUMERATE(NamespaceError, 14) \
  51. __ENUMERATE(InvalidAccessError, 15) \
  52. __ENUMERATE(TypeMismatchError, 17) \
  53. __ENUMERATE(SecurityError, 18) \
  54. __ENUMERATE(NetworkError, 19) \
  55. __ENUMERATE(AbortError, 20) \
  56. __ENUMERATE(URLMismatchError, 21) \
  57. __ENUMERATE(QuotaExceededError, 22) \
  58. __ENUMERATE(TimeoutError, 23) \
  59. __ENUMERATE(InvalidNodeTypeError, 24) \
  60. __ENUMERATE(DataCloneError, 25)
  61. // https://heycam.github.io/webidl/#idl-DOMException-error-names
  62. // Same order as in the spec document, also matches the legacy codes order above.
  63. #define ENUMERATE_DOM_EXCEPTION_ERROR_NAMES \
  64. __ENUMERATE(IndexSizeError) /* Deprecated */ \
  65. __ENUMERATE(HierarchyRequestError) \
  66. __ENUMERATE(WrongDocumentError) \
  67. __ENUMERATE(InvalidCharacterError) \
  68. __ENUMERATE(NoModificationAllowedError) \
  69. __ENUMERATE(NotFoundError) \
  70. __ENUMERATE(NotSupportedError) \
  71. __ENUMERATE(InUseAttributeError) \
  72. __ENUMERATE(InvalidStateError) \
  73. __ENUMERATE(SyntaxError) \
  74. __ENUMERATE(InvalidModificationError) \
  75. __ENUMERATE(NamespaceError) \
  76. __ENUMERATE(InvalidAccessError) /* Deprecated */ \
  77. __ENUMERATE(TypeMismatchError) /* Deprecated */ \
  78. __ENUMERATE(SecurityError) \
  79. __ENUMERATE(NetworkError) \
  80. __ENUMERATE(AbortError) \
  81. __ENUMERATE(URLMismatchError) \
  82. __ENUMERATE(QuotaExceededError) \
  83. __ENUMERATE(TimeoutError) \
  84. __ENUMERATE(InvalidNodeTypeError) \
  85. __ENUMERATE(DataCloneError) \
  86. __ENUMERATE(EncodingError) \
  87. __ENUMERATE(NotReadableError) \
  88. __ENUMERATE(UnknownError) \
  89. __ENUMERATE(ConstraintError) \
  90. __ENUMERATE(DataError) \
  91. __ENUMERATE(TransactionInactiveError) \
  92. __ENUMERATE(ReadOnlyError) \
  93. __ENUMERATE(VersionError) \
  94. __ENUMERATE(OperationError) \
  95. __ENUMERATE(NotAllowedError)
  96. static u16 get_legacy_code_for_name(const FlyString& name)
  97. {
  98. #define __ENUMERATE(ErrorName, code) \
  99. if (name == #ErrorName) \
  100. return code;
  101. ENUMERATE_DOM_EXCEPTION_LEGACY_CODES
  102. #undef __ENUMERATE
  103. return 0;
  104. }
  105. // https://heycam.github.io/webidl/#idl-DOMException
  106. class DOMException final
  107. : public RefCounted<DOMException>
  108. , public Bindings::Wrappable {
  109. public:
  110. using WrapperType = Bindings::DOMExceptionWrapper;
  111. static NonnullRefPtr<DOMException> create(const FlyString& name, const FlyString& message)
  112. {
  113. return adopt(*new DOMException(name, message));
  114. }
  115. // JS constructor has message first, name second
  116. static NonnullRefPtr<DOMException> create_with_global_object(Bindings::WindowObject&, const FlyString& message, const FlyString& name)
  117. {
  118. return adopt(*new DOMException(name, message));
  119. }
  120. const FlyString& name() const { return m_name; }
  121. const FlyString& message() const { return m_message; }
  122. u16 code() const { return get_legacy_code_for_name(m_name); }
  123. protected:
  124. DOMException(const FlyString& name, const FlyString& message)
  125. : m_name(name)
  126. , m_message(message)
  127. {
  128. }
  129. private:
  130. FlyString m_name;
  131. FlyString m_message;
  132. };
  133. #define __ENUMERATE(ErrorName) \
  134. class ErrorName final { \
  135. public: \
  136. static NonnullRefPtr<DOMException> create(const FlyString& message) \
  137. { \
  138. return DOMException::create(#ErrorName, message); \
  139. } \
  140. };
  141. ENUMERATE_DOM_EXCEPTION_ERROR_NAMES
  142. #undef __ENUMERATE
  143. }