1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:07:34 +00:00

WebContent+LibWeb+LibJS: Report exceptions to the JS console

Print exceptions passed to `HTML::report_exception` in the JS console

Refactored `ExceptionReporter`: in order to report exception now
you need to pass the relevant realm in it. For passed `JS::Value`
we now create `JS::Error` object to print value as the error message.
This commit is contained in:
Pavel 2022-10-08 19:38:32 +04:00 committed by Linus Groh
parent 2eb6dbd4f0
commit 40aad77ab1
14 changed files with 86 additions and 58 deletions

View file

@ -202,7 +202,7 @@ JS::VM& main_thread_vm()
// 6. If result is an abrupt completion, then report the exception given by result.[[Value]].
if (result.is_error())
HTML::report_exception(result);
HTML::report_exception(result, finalization_registry.realm());
});
};
@ -275,7 +275,7 @@ JS::VM& main_thread_vm()
// 5. If result is an abrupt completion, then report the exception given by result.[[Value]].
if (result.is_error())
HTML::report_exception(result);
HTML::report_exception(result, job_settings->realm());
});
};
@ -437,7 +437,7 @@ void queue_mutation_observer_microtask(DOM::Document& document)
auto result = WebIDL::invoke_callback(callback, mutation_observer.ptr(), wrapped_records, mutation_observer.ptr());
if (result.is_abrupt())
HTML::report_exception(result);
HTML::report_exception(result, realm);
}
}

View file

@ -1139,7 +1139,7 @@ JS::Value Document::run_javascript(StringView source, StringView filename)
if (result.is_error()) {
// FIXME: I'm sure the spec could tell us something about error propagation here!
HTML::report_exception(result);
HTML::report_exception(result, realm());
return {};
}

View file

@ -114,7 +114,7 @@ bool EventDispatcher::inner_invoke(Event& event, Vector<JS::Handle<DOM::DOMEvent
// If this throws an exception, then:
if (result.is_error()) {
// 1. Report the exception.
HTML::report_exception(result);
HTML::report_exception(result, realm);
// FIXME: 2. Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
}

View file

@ -133,7 +133,7 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
dbgln("no rethrow, stat: {}", evaluation_status.value().value().to_string_without_side_effects());
// 1. Report the exception given by evaluationStatus.[[Value]] for script.
report_exception(evaluation_status);
report_exception(evaluation_status, settings_object().realm());
// 2. Clean up after running script with settings.
settings.clean_up_after_running_script();

View file

@ -239,7 +239,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)
HTML::print_error_from_value(promise.result(), ErrorInPromise::Yes);
HTML::report_exception_to_console(promise.result(), realm(), ErrorInPromise::Yes);
}
});
}

View file

@ -5,15 +5,19 @@
*/
#include <AK/TypeCasts.h>
#include <LibJS/Console.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
namespace Web::HTML {
void print_error_from_value(JS::Value value, ErrorInPromise error_in_promise)
void report_exception_to_console(JS::Value value, JS::Realm& realm, ErrorInPromise error_in_promise)
{
// FIXME: We should probably also report these exceptions to the JS console.
auto& console = realm.intrinsics().console_object()->console();
if (value.is_object()) {
auto& object = value.as_object();
auto& vm = object.vm();
@ -27,24 +31,28 @@ void print_error_from_value(JS::Value value, ErrorInPromise error_in_promise)
}
if (is<JS::Error>(object)) {
auto const& error_value = static_cast<JS::Error const&>(object);
for (auto const& traceback_frame : error_value.traceback()) {
auto const& function_name = traceback_frame.function_name;
auto const& source_range = traceback_frame.source_range;
for (auto& traceback_frame : error_value.traceback()) {
auto& function_name = traceback_frame.function_name;
auto& source_range = traceback_frame.source_range;
dbgln(" {} at {}:{}:{}", function_name, source_range.filename, source_range.start.line, source_range.start.column);
}
console.report_exception(error_value, error_in_promise == ErrorInPromise::Yes);
return;
}
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", value);
dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", value);
}
console.report_exception(*JS::Error::create(realm, value.to_string_without_side_effects()), error_in_promise == ErrorInPromise::Yes);
}
// https://html.spec.whatwg.org/#report-the-exception
void report_exception(JS::Completion const& throw_completion)
void report_exception(JS::Completion const& throw_completion, JS::Realm& realm)
{
// 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);
report_exception_to_console(*throw_completion.value(), realm, ErrorInPromise::No);
}
}

View file

@ -15,14 +15,14 @@ enum class ErrorInPromise {
Yes,
};
void print_error_from_value(JS::Value, ErrorInPromise);
void report_exception(JS::Completion const&);
void report_exception_to_console(JS::Value, JS::Realm&, ErrorInPromise);
void report_exception(JS::Completion const&, JS::Realm&);
template<typename T>
inline void report_exception(JS::ThrowCompletionOr<T> const& result)
inline void report_exception(JS::ThrowCompletionOr<T> const& result, JS::Realm& realm)
{
VERIFY(result.is_throw_completion());
report_exception(result.throw_completion());
report_exception(result.throw_completion(), realm);
}
}

View file

@ -201,7 +201,7 @@ i32 Window::run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS
// 2. If handler is a Function, then invoke handler given arguments with the callback this value set to thisArg. If this throws an exception, catch it, and report the exception.
[&](JS::Handle<WebIDL::CallbackType> callback) {
if (auto result = WebIDL::invoke_callback(*callback, window.ptr(), arguments); result.is_error())
HTML::report_exception(result);
HTML::report_exception(result, weak_window->realm());
},
// 3. Otherwise:
[&](String const& source) {
@ -271,7 +271,7 @@ i32 Window::request_animation_frame_impl(WebIDL::CallbackType& js_callback)
// and if an exception is thrown, report the exception.
if (result.is_error())
HTML::report_exception(result);
HTML::report_exception(result, realm());
});
}
@ -495,11 +495,15 @@ void Window::fire_a_page_transition_event(FlyString const& event_name, bool pers
void Window::queue_microtask_impl(WebIDL::CallbackType& callback)
{
// The queueMicrotask(callback) method must queue a microtask to invoke callback,
HTML::queue_a_microtask(&associated_document(), [&callback]() mutable {
HTML::queue_a_microtask(&associated_document(), [weak_window = make_weak_ptr<Window>(), &callback]() mutable {
JS::GCPtr<Window> window = weak_window.ptr();
if (!window)
return;
auto result = WebIDL::invoke_callback(callback, {});
// and if callback throws an exception, report the exception.
if (result.is_error())
HTML::report_exception(result);
HTML::report_exception(result, window->realm());
});
}
@ -655,7 +659,7 @@ void Window::invoke_idle_callbacks()
// 3. Call callback with deadlineArg as its argument. If an uncaught runtime script error occurs, then report the exception.
auto result = callback->invoke(deadline_arg);
if (result.is_error())
HTML::report_exception(result);
HTML::report_exception(result, realm());
// 4. If window's list of runnable idle callbacks is not empty, queue a task which performs the steps
// in the invoke idle callbacks algorithm with getDeadline and window as a parameters and return from this algorithm
HTML::queue_global_task(HTML::Task::Source::IdleTask, *this, [this]() mutable {