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
Notes: sideshowbarker 2024-07-17 05:47:05 +09:00
14 changed files with 86 additions and 58 deletions

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