1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:18:13 +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

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