DOMException.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/RefCounted.h>
  10. #include <LibWeb/Bindings/Wrappable.h>
  11. namespace Web::DOM {
  12. // The following have a legacy code value but *don't* produce it as
  13. // DOMException.code value when used as name (and are therefore omitted here):
  14. // - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)
  15. // - NoDataAllowedError (NO_DATA_ALLOWED_ERR = 6)
  16. // - ValidationError (VALIDATION_ERR = 16)
  17. #define ENUMERATE_DOM_EXCEPTION_LEGACY_CODES \
  18. __ENUMERATE(IndexSizeError, 1) \
  19. __ENUMERATE(HierarchyRequestError, 3) \
  20. __ENUMERATE(WrongDocumentError, 4) \
  21. __ENUMERATE(InvalidCharacterError, 5) \
  22. __ENUMERATE(NoModificationAllowedError, 7) \
  23. __ENUMERATE(NotFoundError, 8) \
  24. __ENUMERATE(NotSupportedError, 9) \
  25. __ENUMERATE(InUseAttributeError, 10) \
  26. __ENUMERATE(InvalidStateError, 11) \
  27. __ENUMERATE(SyntaxError, 12) \
  28. __ENUMERATE(InvalidModificationError, 13) \
  29. __ENUMERATE(NamespaceError, 14) \
  30. __ENUMERATE(InvalidAccessError, 15) \
  31. __ENUMERATE(TypeMismatchError, 17) \
  32. __ENUMERATE(SecurityError, 18) \
  33. __ENUMERATE(NetworkError, 19) \
  34. __ENUMERATE(AbortError, 20) \
  35. __ENUMERATE(URLMismatchError, 21) \
  36. __ENUMERATE(QuotaExceededError, 22) \
  37. __ENUMERATE(TimeoutError, 23) \
  38. __ENUMERATE(InvalidNodeTypeError, 24) \
  39. __ENUMERATE(DataCloneError, 25)
  40. // https://webidl.spec.whatwg.org/#idl-DOMException-error-names
  41. // Same order as in the spec document, also matches the legacy codes order above.
  42. #define ENUMERATE_DOM_EXCEPTION_ERROR_NAMES \
  43. __ENUMERATE(IndexSizeError) /* Deprecated */ \
  44. __ENUMERATE(HierarchyRequestError) \
  45. __ENUMERATE(WrongDocumentError) \
  46. __ENUMERATE(InvalidCharacterError) \
  47. __ENUMERATE(NoModificationAllowedError) \
  48. __ENUMERATE(NotFoundError) \
  49. __ENUMERATE(NotSupportedError) \
  50. __ENUMERATE(InUseAttributeError) \
  51. __ENUMERATE(InvalidStateError) \
  52. __ENUMERATE(SyntaxError) \
  53. __ENUMERATE(InvalidModificationError) \
  54. __ENUMERATE(NamespaceError) \
  55. __ENUMERATE(InvalidAccessError) /* Deprecated */ \
  56. __ENUMERATE(TypeMismatchError) /* Deprecated */ \
  57. __ENUMERATE(SecurityError) \
  58. __ENUMERATE(NetworkError) \
  59. __ENUMERATE(AbortError) \
  60. __ENUMERATE(URLMismatchError) \
  61. __ENUMERATE(QuotaExceededError) \
  62. __ENUMERATE(TimeoutError) \
  63. __ENUMERATE(InvalidNodeTypeError) \
  64. __ENUMERATE(DataCloneError) \
  65. __ENUMERATE(EncodingError) \
  66. __ENUMERATE(NotReadableError) \
  67. __ENUMERATE(UnknownError) \
  68. __ENUMERATE(ConstraintError) \
  69. __ENUMERATE(DataError) \
  70. __ENUMERATE(TransactionInactiveError) \
  71. __ENUMERATE(ReadOnlyError) \
  72. __ENUMERATE(VersionError) \
  73. __ENUMERATE(OperationError) \
  74. __ENUMERATE(NotAllowedError)
  75. static u16 get_legacy_code_for_name(const FlyString& name)
  76. {
  77. #define __ENUMERATE(ErrorName, code) \
  78. if (name == #ErrorName) \
  79. return code;
  80. ENUMERATE_DOM_EXCEPTION_LEGACY_CODES
  81. #undef __ENUMERATE
  82. return 0;
  83. }
  84. // https://webidl.spec.whatwg.org/#idl-DOMException
  85. class DOMException final
  86. : public RefCounted<DOMException>
  87. , public Bindings::Wrappable {
  88. public:
  89. using WrapperType = Bindings::DOMExceptionWrapper;
  90. static NonnullRefPtr<DOMException> create(const FlyString& name, const FlyString& message)
  91. {
  92. return adopt_ref(*new DOMException(name, message));
  93. }
  94. // JS constructor has message first, name second
  95. static NonnullRefPtr<DOMException> create_with_global_object(Bindings::WindowObject&, const FlyString& message, const FlyString& name)
  96. {
  97. return adopt_ref(*new DOMException(name, message));
  98. }
  99. const FlyString& name() const { return m_name; }
  100. const FlyString& message() const { return m_message; }
  101. u16 code() const { return get_legacy_code_for_name(m_name); }
  102. protected:
  103. DOMException(const FlyString& name, const FlyString& message)
  104. : m_name(name)
  105. , m_message(message)
  106. {
  107. }
  108. private:
  109. FlyString m_name;
  110. FlyString m_message;
  111. };
  112. #define __ENUMERATE(ErrorName) \
  113. class ErrorName final { \
  114. public: \
  115. static NonnullRefPtr<DOMException> create(const FlyString& message) \
  116. { \
  117. return DOMException::create(#ErrorName, message); \
  118. } \
  119. };
  120. ENUMERATE_DOM_EXCEPTION_ERROR_NAMES
  121. #undef __ENUMERATE
  122. }