dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / scripting / exception_reporter.lucb
#==============================================================================================
#
# exception_reporter - HTML: reporting an uncaught exception to the console
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp (lines 1-68) and
# ExceptionReporter.h (lines 1-29). The debug log lines are written; the JS console
# (the realm's %console% intrinsic), the property reads without side effects, the error
# data's stack and JS::Error come with luce-js in phase 3. A DOMException, the one kind
# of thrown object before phase 3, is logged with its name, message and (empty) stack.
#
# Copyright (c) 2022, David Tuin <davidot@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## The " (in promise)" of the log lines.
func html_in_promise_suffix(error_in_promise: HtmlErrorInPromise) -> str:
return " (in promise)" if error_in_promise == HtmlErrorInPromise.yes else ""
## Ported from Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp:18 Web::HTML::report_exception_to_console.
pub func html_report_exception_to_console(value: JsValue, realm: Realm*, error_in_promise: HtmlErrorInPromise):
# auto& console = realm.intrinsics().console_object()->console(); (phase 3)
if js_value_is_object(value):
let object = js_value_as_object(value)
# name and message: object.get_without_side_effects(vm.names.name / vm.names.message). A
# platform object's name and message are accessors on its prototype, so the result is
# not going to be useful, and the value itself is printed.
if let exception = as_if_idl_dom_exception((gc.Cell*)object):
# The result is not going to be useful, let's just print the value. This affects DOMExceptions, for example.
let name = ak.fly_string_bytes_as_string_view(&exception.m_name)
let message_view = ak.utf16_fly_string_view(&exception.m_message)
let message = ak.utf16_view_to_utf8(&message_view) else trap("MUST: Utf16View::to_utf8")
let message_bytes = ak.string_bytes_as_string_view(&message)
ak.dbgln(f"\u{1b}[31;1mUnhandled JavaScript exception{html_in_promise_suffix(error_in_promise)}:\u{1b}[0m {(str)ak.string_view_bytes(&name)}: {(str)ak.string_view_bytes(&message_bytes)}")
# if (auto const* error_data = object.error_data()): a DOMException is its own ErrorData, so its name and
# message are the exception's (above).
# dbgln("{}", error_data->stack_string(JS::CompactTraceback::Yes)): the traceback holds the JS frames that
# were running when the exception was made, the global execution context left out; before phase 3 no
# script runs, so the stack string is empty.
ak.dbgln("")
# console.report_exception(exception_name, exception_message, *error_data, in promise): the realm's
# console (phase 3) has no client to report to before then.
return
else:
trap("unported (P3): report_exception_to_console of a JS object (get_without_side_effects, JS::Value formatting)")
# if (auto const* error_data = object.error_data()) { ... console.report_exception(name, message, *error_data, in promise); return; }
trap("unported (P3): HTML::report_exception_to_console: the error data and JS::Console::report_exception")
else:
trap("unported (P3): report_exception_to_console of a primitive value (JS::Value formatting)")
# auto message = value.to_string_without_side_effects();
# auto error = JS::Error::create(realm, Utf16String::from_utf8(message));
# console.report_exception("Error"_string, message, *error, error_in_promise == ErrorInPromise::Yes);
## Ported from Libraries/LibWeb/HTML/Scripting/ExceptionReporter.cpp:62 Web::HTML::report_exception.
## https://html.spec.whatwg.org/multipage/webappapis.html#report-the-exception
pub func html_report_exception_js_completion(throw_completion_: JsCompletion, realm: Realm*):
assert(js_completion_type(&throw_completion_) == JsCompletionType.throw_)
html_report_exception_to_console(js_completion_value(&throw_completion_), realm, HtmlErrorInPromise.no)
## Ported from Libraries/LibWeb/HTML/Scripting/ExceptionReporter.h:23 Web::HTML::report_exception
## (JS::ThrowCompletionOr<T> const&). A failed `T!` leaves its throw completion in the VM's
## pending-exception slot (DESIGN.md ยง2.10), so the template takes no result: the caller calls
## it from the failed call's `catch`.
pub func html_report_exception_t(realm: Realm*):
# VERIFY(result.is_throw_completion());
html_report_exception_js_completion(vm_take_throw_completion(realm_vm(realm)), realm)