7#pragma push_macro("None")
10#include <Ultralight/CAPI/CAPI_DOMDocument.h>
11#include <Ultralight/CAPI/CAPI_String.h>
12#include <Ultralight/detail/Expected.h>
13#include <Ultralight/dom/detail/Support.h>
123 static Error
PageGone() {
return Error(Kind::kPageGone); }
134 Error result(Kind::kDOMException);
135 result.type_ =
static_cast<ErrorType>(error.code);
136 result.message_ = detail::TakeString(error.message);
137 error.code = kULDOMErrorCode_None;
138 error.message =
nullptr;
152 Error result(Kind::kNative);
154 result.message_ = std::move(
message);
166 bool is_empty()
const {
return kind_ == Kind::kEmpty; }
188 if (kind_ == Kind::kPageGone)
189 return "The page behind this handle is gone (it navigated away, its frame was removed, or "
190 "its View was destroyed).";
191 if (kind_ == Kind::kEmpty)
192 return "A handle used in this call is empty (it was never assigned, was moved from, or "
193 "came from a lookup that found nothing).";
194 if (message_.empty() && kind_ == Kind::kDOMException)
195 return TypeName(type_);
200 friend class detail::ErrorScope;
202 enum class Kind { kNative, kDOMException, kPageGone, kEmpty };
204 explicit Error(Kind kind) : kind_(kind) {}
219 default:
return "A DOM exception occurred.";
223 Kind kind_ = Kind::kNative;
225 std::string message_;
228static_assert(
static_cast<unsigned>(
ErrorType::None) == kULDOMErrorCode_None
232 == kULDOMErrorCode_InvalidCharacterError
234 == kULDOMErrorCode_HierarchyRequestError
236 == kULDOMErrorCode_NotFoundError
238 == kULDOMErrorCode_NoModificationAllowedError
240 == kULDOMErrorCode_InvalidStateError
242 == kULDOMErrorCode_IndexSizeError
244 == kULDOMErrorCode_NotSupportedError
247 == kULDOMErrorCode_InvalidNodeTypeError
249 == kULDOMErrorCode_WrongDocumentError,
250 "dom::ErrorType must mirror ULDOMErrorCode");
255using ultralight::detail::Unexpected;
263template <
typename T,
typename E = Error>
264using Expected = ultralight::detail::Expected<T, E>;
319 requires std::is_default_constructible_v<T>
321 return std::move(result).value_or(T());
332 ErrorScope() =
default;
333 ErrorScope(
const ErrorScope&) =
delete;
334 ErrorScope& operator=(
const ErrorScope&) =
delete;
335 ~ErrorScope() { ulDestroyString(error_.message); }
337 ULDOMError* out() {
return &error_; }
339 bool has_exception()
const {
return error_.code != kULDOMErrorCode_None; }
344 return Error::AdoptDOMException(error_);
345 return Error::PageGone();
348 static Error EmptyHandle() {
return Error(Error::Kind::kEmpty); }
351 ULDOMError error_ = {};
356inline ultralight::detail::Unexpected<Error> EmptyHandleError() {
357 return ultralight::detail::Unexpected<Error>(ErrorScope::EmptyHandle());
362template <
typename Items>
363bool HasEmptyItem(
const Items& items) {
364 for (
const auto& item : items) {
365 if (!item.node && !item.text)
377#pragma pop_macro("None")
static Error AdoptDOMException(ULDOMError &error)
Create an error from a DOM exception reported by the C API.
Definition Error.h:133
bool is_dom_exception() const
Whether or not this error holds a DOM exception.
Definition Error.h:171
static Error PageGone()
Create a page-gone error (is_page_gone() returns true).
Definition Error.h:123
bool is_empty() const
Whether or not this error means a handle holds nothing (see the class notes).
Definition Error.h:166
ErrorType type() const
Get the error's type.
Definition Error.h:179
std::string message() const
Get a readable description of the error.
Definition Error.h:187
static Error Make(ErrorType type, std::string message)
Create a native error.
Definition Error.h:151
bool is_page_gone() const
Whether or not this error means the handle's page is gone.
Definition Error.h:161
Direct C++ access to modify page elements and handle events.
ultralight::detail::Expected< T, E > Expected
A value of type T or an error of type E (like std::expected).
Definition Error.h:264
constexpr Checked_t Checked
Pass to a DOM call to get a dom::Result holding the reason for a failure instead of an empty value.
Definition Error.h:299
T OrEmpty(Result< T > result)
Get a Result's value, or a default-constructed T if it holds an error.
Definition Error.h:320
ErrorType
The kinds of DOM exception a dom::Error can hold (the web's DOMException names, plus TypeError).
Definition Error.h:38
@ HierarchyRequestError
The tree would contain a cycle, or the node can't go there (eg, text directly under the document).
Definition Error.h:46
@ TypeError
An argument has the wrong type (eg, a submitter passed to HTMLFormElement::requestSubmit() that isn't...
Definition Error.h:54
@ IndexSizeError
An index or offset is out of range.
Definition Error.h:52
@ NoModificationAllowedError
The target can't be changed (eg, it has no parent).
Definition Error.h:50
@ InvalidNodeTypeError
The node is the wrong kind for the call (eg, selecting a node that has no parent).
Definition Error.h:57
@ InvalidCharacterError
An invalid tag, attribute, or class name (only the C API reports it, since the typed calls that take ...
Definition Error.h:43
@ NotSupportedError
The operation isn't supported.
Definition Error.h:53
@ None
Not a DOM exception (eg, a page-gone error).
Definition Error.h:39
@ WrongDocumentError
A node or range belongs to a different document.
Definition Error.h:59
@ Unknown
A DOM exception with no code of its own here.
Definition Error.h:40
@ SyntaxError
A malformed selector, or an invalid position for Element::insertAdjacentHTML() and its siblings.
Definition Error.h:41
@ NotFoundError
A node isn't where the call needs it (eg, a reference child that isn't a child).
Definition Error.h:48
@ InvalidStateError
The call isn't valid in the target's current state.
Definition Error.h:51
Expected< T, Error > Result
The result of a DOM operation that can fail (either a T or a dom::Error).
Definition Error.h:277
Root namespace for every public Ultralight type, function, and enumeration.
@ Error
Error icon.
Definition Dialogs.h:57
@ None
Definition Anchor.h:36
The type of dom::Checked.
Definition Error.h:282
constexpr Checked_t()=default
Create the tag (use dom::Checked instead).