LibWeb: Print unhandled rejections the same way as unhandled exceptions

This commit is contained in:
Luke Wilde 2022-06-27 19:53:22 +01:00 committed by Linus Groh
parent 62491cda0b
commit 1e36224321
Notes: sideshowbarker 2024-07-17 18:46:30 +09:00
3 changed files with 23 additions and 12 deletions

View file

@ -10,6 +10,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/PromiseRejectionEvent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Page/Page.h>
@ -232,7 +233,7 @@ void EnvironmentSettingsObject::notify_about_rejected_promises(Badge<EventLoop>)
// This algorithm results in promise rejections being marked as handled or not handled. These concepts parallel handled and not handled script errors.
// If a rejection is still not handled after this, then the rejection may be reported to a developer console.
if (not_handled)
dbgln("WARNING: A promise was rejected without any handlers. promise={:p}, result={}", &promise, promise.result().to_string_without_side_effects());
HTML::print_error_from_value(promise.result(), ErrorInPromise::Yes);
}
});
}

View file

@ -11,24 +11,19 @@
namespace Web::HTML {
// https://html.spec.whatwg.org/#report-the-exception
void report_exception(JS::Completion const& throw_completion)
void print_error_from_value(JS::Value value, ErrorInPromise error_in_promise)
{
// FIXME: This is just old code, and does not strictly follow the spec of report an exception.
// FIXME: We should probably also report these exceptions to the JS console.
VERIFY(throw_completion.type() == JS::Completion::Type::Throw);
VERIFY(throw_completion.value().has_value());
auto thrown_value = *throw_completion.value();
if (thrown_value.is_object()) {
auto& object = thrown_value.as_object();
if (value.is_object()) {
auto& object = value.as_object();
auto& vm = object.vm();
auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
if (name.is_accessor() || message.is_accessor()) {
// The result is not going to be useful, let's just print the value. This affects DOMExceptions, for example.
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", thrown_value);
dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", JS::Value(&object));
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m [{}] {}", name, message);
dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m [{}] {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", name, message);
}
if (is<JS::Error>(object)) {
auto const& error_value = static_cast<JS::Error const&>(object);
@ -39,8 +34,17 @@ void report_exception(JS::Completion const& throw_completion)
}
}
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", thrown_value);
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", value);
}
}
// https://html.spec.whatwg.org/#report-the-exception
void report_exception(JS::Completion const& throw_completion)
{
// FIXME: This is just old code, and does not strictly follow the spec of report an exception.
VERIFY(throw_completion.type() == JS::Completion::Type::Throw);
VERIFY(throw_completion.value().has_value());
print_error_from_value(*throw_completion.value(), ErrorInPromise::No);
}
}

View file

@ -10,6 +10,12 @@
namespace Web::HTML {
enum class ErrorInPromise {
No,
Yes,
};
void print_error_from_value(JS::Value, ErrorInPromise);
void report_exception(JS::Completion const&);
template<typename T>