mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibWeb: Add formatters for WebIDL exception types
This adds formatters for `WebIDL::Exception`, `WebIDL::SimpleException` and `WebIDL::DOMException`. These are useful for displaying the content of errors when debugging.
This commit is contained in:
parent
adc25af8e2
commit
15fe8e7906
Notes:
github-actions[bot]
2025-01-23 20:39:55 +00:00
Author: https://github.com/tcl3 Commit: https://github.com/LadybirdBrowser/ladybird/commit/15fe8e79067 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3340
2 changed files with 62 additions and 0 deletions
|
@ -148,3 +148,15 @@ inline JS::Completion throw_completion(GC::Ref<WebIDL::DOMException> exception)
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Formatter<Web::WebIDL::DOMException> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Web::WebIDL::DOMException const& exception)
|
||||
{
|
||||
return Formatter<FormatString>::format(builder, "[{}]: {}"sv, exception.name(), exception.message());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -134,3 +134,53 @@ public:
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Formatter<Web::WebIDL::SimpleException> : Formatter<StringView> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Web::WebIDL::SimpleException const& exception)
|
||||
{
|
||||
auto message_view = exception.message.visit(
|
||||
[](String const& message) -> StringView {
|
||||
return message.bytes_as_string_view();
|
||||
},
|
||||
[](StringView message) -> StringView {
|
||||
return message;
|
||||
});
|
||||
|
||||
return Formatter<StringView>::format(builder, message_view);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<Web::WebIDL::Exception> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Web::WebIDL::Exception const& exception)
|
||||
{
|
||||
return exception.visit(
|
||||
[&](Web::WebIDL::SimpleException const& simple_exception) -> ErrorOr<void> {
|
||||
return Formatter<FormatString>::format(builder, "{}"sv, simple_exception);
|
||||
},
|
||||
[&](GC::Ref<Web::WebIDL::DOMException> const& dom_exception) -> ErrorOr<void> {
|
||||
return Formatter<FormatString>::format(builder, "{}"sv, *dom_exception);
|
||||
},
|
||||
[&](JS::Completion const& completion) -> ErrorOr<void> {
|
||||
VERIFY(completion.is_error());
|
||||
auto value = *completion.value();
|
||||
|
||||
if (value.is_object()) {
|
||||
auto& object = value.as_object();
|
||||
static const JS::PropertyKey message_property_key { "message" };
|
||||
auto has_message_or_error = object.has_own_property(message_property_key);
|
||||
if (!has_message_or_error.is_error() && has_message_or_error.value()) {
|
||||
auto message_object = object.get_without_side_effects(message_property_key);
|
||||
return Formatter<StringView>::format(builder, message_object.to_string_without_side_effects());
|
||||
}
|
||||
}
|
||||
|
||||
return Formatter<StringView>::format(builder, value.to_string_without_side_effects());
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue