From 96121ddb119e14d36398a4c9589bfecbfd4d7738 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 1 Apr 2021 22:14:12 +0200 Subject: [PATCH] js: Hook up promise rejection tracking callbacks We now leverage the VM's promise rejection tracker callbacks and print a warning in either of these cases: - A promise was rejected without any handlers - A handler was added to an already rejected promise --- Userland/Utilities/js.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 5fe1e4a16f..49e22f313d 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -715,6 +715,26 @@ int main(int argc, char** argv) bool syntax_highlight = !disable_syntax_highlight; vm = JS::VM::create(); + // NOTE: These will print out both warnings when using something like Promise.reject().catch(...) - + // which is, as far as I can tell, correct - a promise is created, rejected without handler, and a + // handler then attached to it. The Node.js REPL doesn't warn in this case, so it's something we + // might want to revisit at a later point and disable warnings for promises created this way. + vm->on_promise_unhandled_rejection = [](auto& promise) { + // FIXME: Optionally make print_value() to print to stderr + out("WARNING: A promise was rejected without any handlers"); + out(" (result: "); + HashTable seen_objects; + print_value(promise.result(), seen_objects); + outln(")"); + }; + vm->on_promise_rejection_handled = [](auto& promise) { + // FIXME: Optionally make print_value() to print to stderr + out("WARNING: A handler was added to an already rejected promise"); + out(" (result: "); + HashTable seen_objects; + print_value(promise.result(), seen_objects); + outln(")"); + }; OwnPtr interpreter; interrupt_interpreter = [&] {