Ladybird+LibJS: Add optional logging of *all* JS exceptions

When running with --log-all-js-exceptions, we will print the message
and backtrace for every single JS exception that is thrown, not just
the ones nobody caught.

This can sometimes be very helpful in debugging sites that swallow
important exceptions.
This commit is contained in:
Andreas Kling
2024-04-16 08:02:41 +02:00
parent 9f4b922f1c
commit 5f9a905793
6 changed files with 43 additions and 1 deletions

View File

@@ -16,6 +16,8 @@
namespace JS {
bool g_log_all_js_exceptions = false;
Completion::Completion(ThrowCompletionOr<Value> const& throw_completion_or_value)
{
if (throw_completion_or_value.is_throw_completion()) {
@@ -122,9 +124,25 @@ ThrowCompletionOr<Value> await(VM& vm, Value value)
return throw_completion(result);
}
static void log_exception(Value value)
{
if (!value.is_object()) {
dbgln("\033[31;1mTHROW!\033[0m {}", value);
return;
}
auto& object = value.as_object();
auto& vm = object.vm();
dbgln("\033[31;1mTHROW!\033[0m {}", object.get(vm.names.message).value());
vm.dump_backtrace();
}
// 6.2.4.2 ThrowCompletion ( value ), https://tc39.es/ecma262/#sec-throwcompletion
Completion throw_completion(Value value)
{
if (g_log_all_js_exceptions)
log_exception(value);
// 1. Return Completion Record { [[Type]]: throw, [[Value]]: value, [[Target]]: empty }.
return { Completion::Type::Throw, value, {} };
}