ExceptionOr.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. template<typename ValueType>
  29. class [[nodiscard]] ExceptionOr {
  30. public:
  31. ExceptionOr()
  32. requires(IsSame<ValueType, Empty>)
  33. : m_result_or_exception(Empty {})
  34. {
  35. }
  36. ExceptionOr(ValueType const& result)
  37. : m_result_or_exception(result)
  38. {
  39. }
  40. ExceptionOr(ValueType&& result)
  41. : m_result_or_exception(move(result))
  42. {
  43. }
  44. // Allows implicit construction of ExceptionOr<T> from a type U if T(U) is a supported constructor.
  45. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
  46. // Disabled for POD types to avoid weird conversion shenanigans.
  47. template<typename WrappedValueType>
  48. ExceptionOr(WrappedValueType result)
  49. requires(!IsPOD<ValueType>)
  50. : m_result_or_exception(ValueType { move(result) })
  51. {
  52. }
  53. ExceptionOr(JS::NonnullGCPtr<DOMException> exception)
  54. : m_result_or_exception(exception)
  55. {
  56. }
  57. ExceptionOr(SimpleException exception)
  58. : m_result_or_exception(move(exception))
  59. {
  60. }
  61. ExceptionOr(JS::Completion exception)
  62. : m_result_or_exception(move(exception))
  63. {
  64. auto const& completion = m_result_or_exception.template get<JS::Completion>();
  65. VERIFY(completion.is_error());
  66. }
  67. ExceptionOr(Variant<SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion> exception)
  68. : m_result_or_exception(move(exception))
  69. {
  70. if (auto* completion = m_result_or_exception.template get_pointer<JS::Completion>())
  71. VERIFY(completion->is_error());
  72. }
  73. ExceptionOr(ExceptionOr&& other) = default;
  74. ExceptionOr(ExceptionOr const& other) = default;
  75. ~ExceptionOr() = default;
  76. ValueType& value()
  77. requires(!IsSame<ValueType, Empty>)
  78. {
  79. return m_result_or_exception.template get<ValueType>();
  80. }
  81. ValueType release_value()
  82. {
  83. return move(m_result_or_exception.template get<ValueType>());
  84. }
  85. Variant<SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion> exception() const
  86. {
  87. return m_result_or_exception.template downcast<SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion>();
  88. }
  89. bool is_exception() const
  90. {
  91. return !m_result_or_exception.template has<ValueType>();
  92. }
  93. ValueType release_value_but_fixme_should_propagate_errors()
  94. {
  95. VERIFY(!is_error());
  96. return release_value();
  97. }
  98. // These are for compatibility with the TRY() macro in AK.
  99. [[nodiscard]] bool is_error() const { return is_exception(); }
  100. Variant<SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion> release_error() { return exception(); }
  101. private:
  102. // https://webidl.spec.whatwg.org/#idl-exceptions
  103. Variant<ValueType, SimpleException, JS::NonnullGCPtr<DOMException>, JS::Completion> m_result_or_exception;
  104. };
  105. template<>
  106. class [[nodiscard]] ExceptionOr<void> : public ExceptionOr<Empty> {
  107. public:
  108. using ExceptionOr<Empty>::ExceptionOr;
  109. };
  110. }