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:
Tim Ledbetter 2025-01-22 12:08:32 +00:00 committed by Andreas Kling
parent adc25af8e2
commit 15fe8e7906
Notes: github-actions[bot] 2025-01-23 20:39:55 +00:00
2 changed files with 62 additions and 0 deletions

View file

@ -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());
}
};
}

View file

@ -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());
});
}
};
}