diff options
author | Linus Groh <mail@linusgroh.de> | 2021-04-01 22:14:12 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-02 10:47:40 +0200 |
commit | 96121ddb119e14d36398a4c9589bfecbfd4d7738 (patch) | |
tree | ebb9a6469314cb5469e0ec0d81022ed2baaf7049 /Userland/Utilities | |
parent | 9da13302abf18fdef962507c14568ede817ca43d (diff) | |
download | serenity-96121ddb119e14d36398a4c9589bfecbfd4d7738.zip |
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
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/js.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
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<JS::Object*> 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<JS::Object*> seen_objects; + print_value(promise.result(), seen_objects); + outln(")"); + }; OwnPtr<JS::Interpreter> interpreter; interrupt_interpreter = [&] { |