diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-03-22 07:44:33 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-03-22 12:09:27 +0000 |
commit | 2d34216628c235465e630292ef067a2b070d0c27 (patch) | |
tree | a6d57529f5e31b1e01d1a4d5294dcbcfcb73ca8d /Userland/Libraries | |
parent | f36f9c106bfa3066110948215008b4bfb23256fa (diff) | |
download | serenity-2d34216628c235465e630292ef067a2b070d0c27.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/ExceptionOr.h | 11 |
1 files 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<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 |