소스 검색

LibWeb: Make DOM::ExceptionOr compatible with the TRY macro

This will help reduce the quite repetitive pattern of:

    auto result_or_error = dom_node->do_something();
    if (result_or_error.is_exception())
        return result_or_error.exception();
    auto result = result_or_error.release_value();

Similar to LibJS completions, this adds an alias to the error accessors.
This also removes the requirement on release_value() for ValueType to
not be Empty, which we also had to do for TRY compatibility in LibJS.
Timothy Flynn 3 년 전
부모
커밋
2d34216628
1개의 변경된 파일9개의 추가작업 그리고 2개의 파일을 삭제
  1. 9 2
      Userland/Libraries/LibWeb/DOM/ExceptionOr.h

+ 9 - 2
Userland/Libraries/LibWeb/DOM/ExceptionOr.h

@@ -34,7 +34,10 @@ struct SimpleException {
 template<typename ValueType>
 class ExceptionOr {
 public:
-    ExceptionOr() requires(IsSame<ValueType, Empty>) = default;
+    ExceptionOr() requires(IsSame<ValueType, Empty>)
+        : m_result(Empty {})
+    {
+    }
 
     ExceptionOr(const ValueType& result)
         : m_result(result)
@@ -70,7 +73,7 @@ public:
         return m_result.value();
     }
 
-    ValueType release_value() requires(!IsSame<ValueType, Empty>)
+    ValueType release_value()
     {
         return m_result.release_value();
     }
@@ -85,6 +88,10 @@ public:
         return !m_exception.template has<Empty>();
     }
 
+    // These are for compatibility with the TRY() macro in AK.
+    [[nodiscard]] bool is_error() const { return is_exception(); }
+    Variant<SimpleException, NonnullRefPtr<DOMException>> release_error() { return exception(); }
+
 private:
     Optional<ValueType> m_result;
     // https://webidl.spec.whatwg.org/#idl-exceptions