use std::any::Any;
use std::panic::AssertUnwindSafe;
use futures::FutureExt;
use polars_error::{PolarsError, PolarsResult};
#[derive(Clone)]
pub struct ErrorCapture {
tx: tokio::sync::mpsc::Sender<ErrorMessage>,
}
impl ErrorCapture {
pub fn new() -> (Self, ErrorHandle) {
let (tx, rx) = tokio::sync::mpsc::channel(1);
(Self { tx }, ErrorHandle { rx })
}
pub async fn wrap_future<F, O>(self, fut: F)
where
F: Future<Output = PolarsResult<O>>,
{
let err: Result<(), tokio::sync::mpsc::error::TrySendError<ErrorMessage>> =
match AssertUnwindSafe(fut).catch_unwind().await {
Ok(Ok(_)) => return,
Ok(Err(err)) => self.tx.try_send(ErrorMessage::Error(err)),
Err(panic) => self.tx.try_send(ErrorMessage::Panic(panic)),
};
drop(err);
}
}
enum ErrorMessage {
Error(PolarsError),
Panic(Box<dyn Any + Send + 'static>),
}
pub struct ErrorHandle {
rx: tokio::sync::mpsc::Receiver<ErrorMessage>,
}
impl ErrorHandle {
pub fn has_errored(&self) -> bool {
!self.rx.is_empty()
}
pub async fn join(self) -> PolarsResult<()> {
let ErrorHandle { mut rx } = self;
match rx.recv().await {
None => Ok(()),
Some(ErrorMessage::Error(e)) => Err(e),
Some(ErrorMessage::Panic(panic)) => std::panic::resume_unwind(panic),
}
}
}