use crate::distributed::{ErrorStoreError, WorkflowError};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
#[async_trait]
pub trait ErrorStore {
async fn record_error(&self, run_id: &str, error: WorkflowError)
-> Result<(), ErrorStoreError>;
async fn get_errors(&self, run_id: &str) -> Result<Vec<WorkflowError>, ErrorStoreError>;
}
#[derive(Clone, Default)]
pub struct InMemoryErrorStore {
inner: Arc<Mutex<HashMap<String, Vec<WorkflowError>>>>,
}
#[async_trait]
impl ErrorStore for InMemoryErrorStore {
async fn record_error(
&self,
run_id: &str,
error: WorkflowError,
) -> Result<(), ErrorStoreError> {
let mut map = self.inner.lock().await;
map.entry(run_id.to_string()).or_default().push(error);
Ok(())
}
async fn get_errors(&self, run_id: &str) -> Result<Vec<WorkflowError>, ErrorStoreError> {
let map = self.inner.lock().await;
Ok(map.get(run_id).cloned().unwrap_or_default())
}
}