Skip to main content

agent_effects_store/
error.rs

1use std::error::Error;
2use std::time::SystemTime;
3
4use crate::id::{EffectId, WorkerId};
5use crate::state::InvalidTransition;
6
7/// Why a store operation failed.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum StoreError {
11    /// No record has this id.
12    #[error("effect {0} not found")]
13    NotFound(EffectId),
14    /// The transition is not legal from the record's status.
15    #[error(transparent)]
16    InvalidTransition(#[from] InvalidTransition),
17    /// The record changed since the caller read it.
18    #[error("effect changed concurrently: expected version {expected}, found {actual}")]
19    VersionConflict {
20        /// The version the caller expected.
21        expected: u64,
22        /// The record's current version.
23        actual: u64,
24    },
25    /// Another worker, or another task of this one, holds a live lease.
26    #[error("lease is held by {owner} until {expires_at:?}")]
27    LeaseHeld {
28        /// The current holder.
29        owner: WorkerId,
30        /// When its lease lapses unless renewed.
31        expires_at: SystemTime,
32    },
33    /// The caller's lease expired or was taken over. The caller must stop
34    /// acting on the effect.
35    #[error("lease lost: it expired or another worker took over")]
36    LeaseLost,
37    /// The storage backend failed.
38    #[error("store backend error: {0}")]
39    Backend(#[source] Box<dyn Error + Send + Sync>),
40}
41
42impl StoreError {
43    /// Wraps a backend error.
44    pub fn backend(error: impl Into<Box<dyn Error + Send + Sync>>) -> Self {
45        Self::Backend(error.into())
46    }
47}