diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-06-27 19:53:22 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-29 21:21:50 +0100 |
commit | 1e3622432107dc5b47242da88479e0c189e7a488 (patch) | |
tree | 8fc8e66bcad1d5dc80e653cdcdca0e1ffb39d69b /Userland/Libraries/LibWeb/HTML | |
parent | 62491cda0b9e8aa55067aa794d58a79b965f6f46 (diff) | |
download | serenity-1e3622432107dc5b47242da88479e0c189e7a488.zip |
LibWeb: Print unhandled rejections the same way as unhandled exceptions
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
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 <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); } }); } 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<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); +} + } 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<typename T> |