mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:17:44 +00:00
LibWeb: Move ExceptionOr from DOM/ to WebIDL/
This is a concept fully defined in the Web IDL spec and doesn't belong in the DOM directory/namespace - not even DOMException, despite the name :^)
This commit is contained in:
parent
c0eda77928
commit
ad04d7ac9b
107 changed files with 441 additions and 440 deletions
108
Userland/Libraries/LibWeb/WebIDL/ExceptionOr.h
Normal file
108
Userland/Libraries/LibWeb/WebIDL/ExceptionOr.h
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <LibWeb/DOM/DOMException.h>
|
||||
|
||||
namespace Web::WebIDL {
|
||||
|
||||
#define ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E) \
|
||||
E(EvalError) \
|
||||
E(RangeError) \
|
||||
E(ReferenceError) \
|
||||
E(TypeError) \
|
||||
E(URIError)
|
||||
|
||||
#define E(x) x,
|
||||
enum class SimpleExceptionType {
|
||||
ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E)
|
||||
};
|
||||
#undef E
|
||||
|
||||
struct SimpleException {
|
||||
SimpleExceptionType type;
|
||||
String message;
|
||||
};
|
||||
|
||||
template<typename ValueType>
|
||||
class ExceptionOr {
|
||||
public:
|
||||
ExceptionOr() requires(IsSame<ValueType, Empty>)
|
||||
: m_result(Empty {})
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionOr(ValueType const& result)
|
||||
: m_result(result)
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionOr(ValueType&& result)
|
||||
: m_result(move(result))
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionOr(JS::NonnullGCPtr<DOM::DOMException> exception)
|
||||
: m_exception(move(exception))
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionOr(SimpleException exception)
|
||||
: m_exception(move(exception))
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionOr(Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> exception)
|
||||
: m_exception(move(exception).template downcast<Empty, SimpleException, JS::NonnullGCPtr<DOM::DOMException>>())
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionOr(ExceptionOr&& other) = default;
|
||||
ExceptionOr(ExceptionOr const& other) = default;
|
||||
~ExceptionOr() = default;
|
||||
|
||||
ValueType& value() requires(!IsSame<ValueType, Empty>)
|
||||
{
|
||||
return m_result.value();
|
||||
}
|
||||
|
||||
ValueType release_value()
|
||||
{
|
||||
return m_result.release_value();
|
||||
}
|
||||
|
||||
Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> exception() const
|
||||
{
|
||||
return m_exception.template downcast<SimpleException, JS::NonnullGCPtr<DOM::DOMException>>();
|
||||
}
|
||||
|
||||
bool is_exception() const
|
||||
{
|
||||
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, JS::NonnullGCPtr<DOM::DOMException>> release_error() { return exception(); }
|
||||
|
||||
private:
|
||||
Optional<ValueType> m_result;
|
||||
|
||||
// https://webidl.spec.whatwg.org/#idl-exceptions
|
||||
Variant<Empty, SimpleException, JS::NonnullGCPtr<DOM::DOMException>> m_exception {};
|
||||
};
|
||||
|
||||
template<>
|
||||
class ExceptionOr<void> : public ExceptionOr<Empty> {
|
||||
public:
|
||||
using ExceptionOr<Empty>::ExceptionOr;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue