floxide_core/distributed/
error_store.rs

1//! Error store for distributed workflow runs.
2//!
3//! This module defines the ErrorStore trait for recording and retrieving workflow errors,
4//! and provides an in-memory implementation for testing and local development.
5
6use crate::distributed::{ErrorStoreError, WorkflowError};
7use async_trait::async_trait;
8use std::collections::HashMap;
9use std::sync::Arc;
10use tokio::sync::Mutex;
11
12/// Trait for a distributed workflow error store.
13///
14/// Implementations record and retrieve errors encountered during workflow execution.
15#[async_trait]
16pub trait ErrorStore {
17    /// Record an error for a workflow run.
18    async fn record_error(&self, run_id: &str, error: WorkflowError)
19        -> Result<(), ErrorStoreError>;
20    /// Get all errors for a workflow run.
21    async fn get_errors(&self, run_id: &str) -> Result<Vec<WorkflowError>, ErrorStoreError>;
22}
23
24/// In-memory implementation of ErrorStore for testing and local development.
25#[derive(Clone, Default)]
26pub struct InMemoryErrorStore {
27    inner: Arc<Mutex<HashMap<String, Vec<WorkflowError>>>>,
28}
29
30#[async_trait]
31impl ErrorStore for InMemoryErrorStore {
32    async fn record_error(
33        &self,
34        run_id: &str,
35        error: WorkflowError,
36    ) -> Result<(), ErrorStoreError> {
37        let mut map = self.inner.lock().await;
38        map.entry(run_id.to_string()).or_default().push(error);
39        Ok(())
40    }
41    async fn get_errors(&self, run_id: &str) -> Result<Vec<WorkflowError>, ErrorStoreError> {
42        let map = self.inner.lock().await;
43        Ok(map.get(run_id).cloned().unwrap_or_default())
44    }
45}