Skip to main content

rightkit_memory_client/backend/
error.rs

1//! Typed failure values for every memory backend operation.
2
3use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum MemoryBackendErrorKind {
7    /// No authenticated, identity-bound Membrane backend is resident.
8    BackendUnavailable,
9    /// A durable mutation may have committed; reconcile its idempotency key.
10    CommitUnknown,
11    Unavailable,
12    Invalid,
13    Denied,
14    Timeout,
15    Cancelled,
16    Incompatible,
17    Store,
18    Internal,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum MemoryBackendError {
23    BackendUnavailable { code: String, message: String },
24    CommitUnknown { code: String, message: String },
25    Unavailable { code: String, message: String },
26    Invalid { code: String, message: String },
27    Denied { code: String, message: String },
28    Timeout { code: String, message: String },
29    Cancelled { code: String, message: String },
30    Incompatible { code: String, message: String },
31    Store { code: String, message: String },
32    Internal { code: String, message: String },
33}
34
35impl MemoryBackendError {
36    pub fn kind(&self) -> MemoryBackendErrorKind {
37        match self {
38            Self::BackendUnavailable { .. } => MemoryBackendErrorKind::BackendUnavailable,
39            Self::CommitUnknown { .. } => MemoryBackendErrorKind::CommitUnknown,
40            Self::Unavailable { .. } => MemoryBackendErrorKind::Unavailable,
41            Self::Invalid { .. } => MemoryBackendErrorKind::Invalid,
42            Self::Denied { .. } => MemoryBackendErrorKind::Denied,
43            Self::Timeout { .. } => MemoryBackendErrorKind::Timeout,
44            Self::Cancelled { .. } => MemoryBackendErrorKind::Cancelled,
45            Self::Incompatible { .. } => MemoryBackendErrorKind::Incompatible,
46            Self::Store { .. } => MemoryBackendErrorKind::Store,
47            Self::Internal { .. } => MemoryBackendErrorKind::Internal,
48        }
49    }
50
51    pub fn code(&self) -> &str {
52        match self {
53            Self::BackendUnavailable { code, .. }
54            | Self::CommitUnknown { code, .. }
55            | Self::Unavailable { code, .. }
56            | Self::Invalid { code, .. }
57            | Self::Denied { code, .. }
58            | Self::Timeout { code, .. }
59            | Self::Cancelled { code, .. }
60            | Self::Incompatible { code, .. }
61            | Self::Store { code, .. }
62            | Self::Internal { code, .. } => code,
63        }
64    }
65
66    pub fn message(&self) -> &str {
67        match self {
68            Self::BackendUnavailable { message, .. }
69            | Self::CommitUnknown { message, .. }
70            | Self::Unavailable { message, .. }
71            | Self::Invalid { message, .. }
72            | Self::Denied { message, .. }
73            | Self::Timeout { message, .. }
74            | Self::Cancelled { message, .. }
75            | Self::Incompatible { message, .. }
76            | Self::Store { message, .. }
77            | Self::Internal { message, .. } => message,
78        }
79    }
80
81    pub fn cancelled(message: impl Into<String>) -> Self {
82        Self::Cancelled {
83            code: "cancelled".into(),
84            message: message.into(),
85        }
86    }
87
88    pub fn timeout(message: impl Into<String>) -> Self {
89        Self::Timeout {
90            code: "timeout".into(),
91            message: message.into(),
92        }
93    }
94
95    /// Hub is not resident; callers may continue non-memory routes but must
96    /// not fall back to a local memory authority.
97    pub fn backend_unavailable(message: impl Into<String>) -> Self {
98        Self::BackendUnavailable {
99            code: "backend_unavailable".into(),
100            message: message.into(),
101        }
102    }
103
104    /// A mutating request may have reached Hub before transport loss. It is
105    /// unsafe to report a normal failure or retry with a new key.
106    pub fn commit_unknown(message: impl Into<String>) -> Self {
107        Self::CommitUnknown {
108            code: "commit_unknown".into(),
109            message: message.into(),
110        }
111    }
112}
113
114impl fmt::Display for MemoryBackendError {
115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116        write!(f, "{}: {}", self.code(), self.message())
117    }
118}
119
120impl std::error::Error for MemoryBackendError {}