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
Notes: sideshowbarker 2024-07-17 16:56:06 +09:00

View File

@ -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