1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:47:35 +00:00

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
This commit is contained in:
Linus Groh 2021-04-01 22:14:12 +02:00 committed by Andreas Kling
parent 9da13302ab
commit 96121ddb11

View file

@ -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 = [&] {