[oden-js] Track rejected promises, panic on unhandled rejections

This commit is contained in:
John Doty 2023-07-25 06:39:01 -07:00
parent 17c701a7d6
commit 22327a71b3
4 changed files with 108 additions and 14 deletions

View file

@ -1,4 +1,4 @@
use crate::{ContextRef, ValueResult};
use crate::{ContextRef, ValueRef, ValueResult};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::Sender;
@ -72,3 +72,53 @@ impl Drop for Promise {
assert!(self.complete);
}
}
pub trait RejectedPromiseTracker {
fn on_rejected_promise(
&self,
ctx: &ContextRef,
promise: &ValueRef,
reason: &ValueRef,
is_handled: bool,
);
}
impl<T> RejectedPromiseTracker for T
where
T: Fn(&ContextRef, &ValueRef, &ValueRef, bool) -> (),
{
fn on_rejected_promise(
&self,
ctx: &ContextRef,
promise: &ValueRef,
reason: &ValueRef,
is_handled: bool,
) {
self(ctx, promise, reason, is_handled);
}
}
pub struct DefaultRejectedPromiseTracker {}
impl DefaultRejectedPromiseTracker {
pub fn new() -> Self {
DefaultRejectedPromiseTracker {}
}
}
impl RejectedPromiseTracker for DefaultRejectedPromiseTracker {
fn on_rejected_promise(
&self,
ctx: &ContextRef,
_promise: &ValueRef,
reason: &ValueRef,
is_handled: bool,
) {
if !is_handled {
let reason_str = reason.to_string(ctx).expect(
"Unhandled rejected promise: reason unknown: unable to convert reason to string",
);
panic!("Unhandled rejected promise: {reason_str}");
}
}
}