ExceptionOr.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/Optional.h>
  9. #include <AK/RefPtr.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibWeb/WebIDL/DOMException.h>
  12. namespace Web::WebIDL {
  13. #define ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E) \
  14. E(EvalError) \
  15. E(RangeError) \
  16. E(ReferenceError) \
  17. E(TypeError) \
  18. E(URIError)
  19. #define E(x) x,
  20. enum class SimpleExceptionType {
  21. ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E)
  22. };
  23. #undef E
  24. struct SimpleException {
  25. SimpleExceptionType type;
  26. Variant<String, StringView> message;
  27. };
  28. using Exception = Variant<SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion>;
  29. template<typename ValueType>
  30. class [[nodiscard]] ExceptionOr {
  31. public:
  32. ExceptionOr()
  33. requires(IsSame<ValueType, Empty>)
  34. : m_result_or_exception(Empty {})
  35. {
  36. }
  37. ExceptionOr(ValueType const& result)
  38. : m_result_or_exception(result)
  39. {
  40. }
  41. ExceptionOr(ValueType&& result)
  42. : m_result_or_exception(move(result))
  43. {
  44. }
  45. // Allows implicit construction of ExceptionOr<T> from a type U if T(U) is a supported constructor.
  46. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
  47. // Disabled for POD types to avoid weird conversion shenanigans.
  48. template<typename WrappedValueType>
  49. ExceptionOr(WrappedValueType result)
  50. requires(!IsPOD<ValueType>)
  51. : m_result_or_exception(ValueType { move(result) })
  52. {
  53. }
  54. ExceptionOr(JS::NonnullGCPtr<DOMException> exception)
  55. : m_result_or_exception(exception)
  56. {
  57. }
  58. ExceptionOr(SimpleException exception)
  59. : m_result_or_exception(move(exception))
  60. {
  61. }
  62. ExceptionOr(JS::Completion exception)
  63. : m_result_or_exception(move(exception))
  64. {
  65. auto const& completion = m_result_or_exception.template get<JS::Completion>();
  66. VERIFY(completion.is_error());
  67. }
  68. ExceptionOr(Exception exception)
  69. : m_result_or_exception(move(exception))
  70. {
  71. if (auto* completion = m_result_or_exception.template get_pointer<JS::Completion>())
  72. VERIFY(completion->is_error());
  73. }
  74. ExceptionOr(ExceptionOr&& other) = default;
  75. ExceptionOr(ExceptionOr const& other) = default;
  76. ~ExceptionOr() = default;
  77. ValueType& value()
  78. requires(!IsSame<ValueType, Empty>)
  79. {
  80. return m_result_or_exception.template get<ValueType>();
  81. }
  82. ValueType release_value()
  83. {
  84. return move(m_result_or_exception.template get<ValueType>());
  85. }
  86. Exception exception() const
  87. {
  88. return m_result_or_exception.template downcast<SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion>();
  89. }
  90. bool is_exception() const
  91. {
  92. return !m_result_or_exception.template has<ValueType>();
  93. }
  94. ValueType release_value_but_fixme_should_propagate_errors()
  95. {
  96. VERIFY(!is_error());
  97. return release_value();
  98. }
  99. // These are for compatibility with the TRY() macro in AK.
  100. [[nodiscard]] bool is_error() const { return is_exception(); }
  101. Exception release_error() { return exception(); }
  102. private:
  103. // https://webidl.spec.whatwg.org/#idl-exceptions
  104. Variant<ValueType, SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion> m_result_or_exception;
  105. };
  106. template<>
  107. class [[nodiscard]] ExceptionOr<void> : public ExceptionOr<Empty> {
  108. public:
  109. using ExceptionOr<Empty>::ExceptionOr;
  110. };
  111. }