From 1e3622432107dc5b47242da88479e0c189e7a488 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Mon, 27 Jun 2022 19:53:22 +0100 Subject: [PATCH] LibWeb: Print unhandled rejections the same way as unhandled exceptions --- .../LibWeb/HTML/Scripting/Environments.cpp | 3 ++- .../HTML/Scripting/ExceptionReporter.cpp | 26 +++++++++++-------- .../LibWeb/HTML/Scripting/ExceptionReporter.h | 6 +++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp index 23abf2c1f8..da1945f979 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -232,7 +233,7 @@ void EnvironmentSettingsObject::notify_about_rejected_promises(Badge) // 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); } }); } diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp index c5e4802cdd..bea5fbc44b 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp @@ -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(object)) { auto const& error_value = static_cast(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); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h b/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h index dc4aad44c8..f7d7061772 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h @@ -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