1use core::fmt;
4
5use oxrdf::NamedNode;
6
7use crate::journal::Position;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
11pub enum StoreError {
12 Unavailable(String),
15 Invalid(String),
17 Unsupported(String),
19 Backend(String),
21}
22
23impl StoreError {
24 pub fn invalid(msg: impl Into<String>) -> Self {
26 StoreError::Invalid(msg.into())
27 }
28
29 pub fn backend(err: impl fmt::Display) -> Self {
31 StoreError::Backend(err.to_string())
32 }
33
34 pub fn is_transient(&self) -> bool {
36 matches!(self, StoreError::Unavailable(_))
37 }
38}
39
40impl fmt::Display for StoreError {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 StoreError::Unavailable(m) => write!(f, "store unavailable: {m}"),
44 StoreError::Invalid(m) => write!(f, "invalid request: {m}"),
45 StoreError::Unsupported(m) => write!(f, "unsupported: {m}"),
46 StoreError::Backend(m) => write!(f, "backend error: {m}"),
47 }
48 }
49}
50
51impl std::error::Error for StoreError {}
52
53#[derive(Clone, Debug, PartialEq, Eq)]
58pub enum AppendError {
59 Conflict {
61 expected: Position,
63 actual: Position,
65 },
66 Fenced {
68 scope: NamedNode,
70 epoch: u64,
72 highest: u64,
74 },
75 Unsupported(String),
78 Invalid(String),
81 Storage(StoreError),
83}
84
85impl AppendError {
86 pub fn is_conflict(&self) -> bool {
88 matches!(self, AppendError::Conflict { .. })
89 }
90
91 pub fn is_fenced(&self) -> bool {
93 matches!(self, AppendError::Fenced { .. })
94 }
95}
96
97impl fmt::Display for AppendError {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 match self {
100 AppendError::Conflict { expected, actual } => write!(
101 f,
102 "append conflict: expected position {}, journal is at {}",
103 expected.get(),
104 actual.get()
105 ),
106 AppendError::Fenced {
107 scope,
108 epoch,
109 highest,
110 } => write!(
111 f,
112 "append fenced: epoch {epoch} for {scope} is below the accepted epoch {highest}"
113 ),
114 AppendError::Unsupported(m) => write!(f, "unsupported append: {m}"),
115 AppendError::Invalid(m) => write!(f, "invalid append: {m}"),
116 AppendError::Storage(e) => write!(f, "append failed: {e}"),
117 }
118 }
119}
120
121impl std::error::Error for AppendError {
122 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
123 match self {
124 AppendError::Storage(e) => Some(e),
125 _ => None,
126 }
127 }
128}
129
130impl From<StoreError> for AppendError {
131 fn from(e: StoreError) -> Self {
132 match e {
133 StoreError::Invalid(m) => AppendError::Invalid(m),
134 StoreError::Unsupported(m) => AppendError::Unsupported(m),
135 other => AppendError::Storage(other),
136 }
137 }
138}