From 2d34216628c235465e630292ef067a2b070d0c27 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 22 Mar 2022 07:44:33 -0400 Subject: [PATCH] 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. --- Userland/Libraries/LibWeb/DOM/ExceptionOr.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/ExceptionOr.h b/Userland/Libraries/LibWeb/DOM/ExceptionOr.h index ec889a3337..e741cbceba 100644 --- a/Userland/Libraries/LibWeb/DOM/ExceptionOr.h +++ b/Userland/Libraries/LibWeb/DOM/ExceptionOr.h @@ -34,7 +34,10 @@ struct SimpleException { template class ExceptionOr { public: - ExceptionOr() requires(IsSame) = default; + ExceptionOr() requires(IsSame) + : 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 release_value() { return m_result.release_value(); } @@ -85,6 +88,10 @@ public: return !m_exception.template has(); } + // These are for compatibility with the TRY() macro in AK. + [[nodiscard]] bool is_error() const { return is_exception(); } + Variant> release_error() { return exception(); } + private: Optional m_result; // https://webidl.spec.whatwg.org/#idl-exceptions