1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

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.
This commit is contained in:
Timothy Flynn 2022-03-22 07:44:33 -04:00 committed by Linus Groh
parent f36f9c106b
commit 2d34216628

View file

@ -34,7 +34,10 @@ struct SimpleException {
template<typename ValueType> template<typename ValueType>
class ExceptionOr { class ExceptionOr {
public: public:
ExceptionOr() requires(IsSame<ValueType, Empty>) = default; ExceptionOr() requires(IsSame<ValueType, Empty>)
: m_result(Empty {})
{
}
ExceptionOr(const ValueType& result) ExceptionOr(const ValueType& result)
: m_result(result) : m_result(result)
@ -70,7 +73,7 @@ public:
return m_result.value(); return m_result.value();
} }
ValueType release_value() requires(!IsSame<ValueType, Empty>) ValueType release_value()
{ {
return m_result.release_value(); return m_result.release_value();
} }
@ -85,6 +88,10 @@ public:
return !m_exception.template has<Empty>(); 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: private:
Optional<ValueType> m_result; Optional<ValueType> m_result;
// https://webidl.spec.whatwg.org/#idl-exceptions // https://webidl.spec.whatwg.org/#idl-exceptions