1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::error::Error;
use std::fmt;
use crate::lock::{LockError, RetryClass};
use crate::table::TableStoreError;
use crate::EventRecordError;
#[derive(Debug)]
#[non_exhaustive]
pub enum RepositoryError {
LockPoisoned(&'static str),
Lock(LockError),
ConcurrentWrite {
id: String,
expected: u64,
actual: u64,
},
DuplicateStreamInBatch {
id: String,
},
DuplicateOutboxMessageInBatch {
id: String,
},
/// A consumer inbox receipt `(consumer, message_id)` was already recorded.
/// The commit is rolled back so the consumer's effects are not double-applied;
/// the message has already been processed (an at-least-once replay).
DuplicateInboxReceipt {
consumer: String,
message_id: String,
},
/// A consumer inbox receipt had an empty `consumer` or `message_id`. Rejected
/// uniformly across backends before any write (the relational `CHECK`
/// constraints are a defense-in-depth backstop).
InvalidInboxReceipt {
consumer: String,
message_id: String,
},
/// A raw/legacy repository batch targeted a causal-owned read-model table.
CausalWriteRequired {
table: String,
},
InvalidStreamIdentity {
aggregate_type: String,
aggregate_id: String,
reason: String,
},
NotFound {
id: String,
},
InvalidState {
id: String,
expected: &'static str,
actual: String,
},
Replay(String),
Model(String),
/// A storage backend (event store, read model, snapshot store) failed to
/// complete an operation. Unlike [`RepositoryError::Model`] — a deterministic
/// modeling/decoding fault — this carries an explicit retry classification so
/// callers can distinguish a transient outage (connection refused, pool
/// timeout, `SQLITE_BUSY`) from a deterministic failure (constraint
/// violation, malformed row) without string-sniffing the message.
///
/// The optional `source` preserves the underlying error for diagnostics and
/// dead-letter metadata; it is exposed through [`Error::source`].
Storage {
/// The operation that failed (e.g. `"sqlite insert event"`).
operation: String,
/// Whether retrying the same operation may succeed.
retryable: bool,
/// The underlying backend error, if available.
source: Option<Box<dyn Error + Send + Sync>>,
},
}
impl RepositoryError {
/// Construct a retryable storage failure carrying its source.
pub fn retryable_storage(
operation: impl Into<String>,
source: impl Error + Send + Sync + 'static,
) -> Self {
RepositoryError::Storage {
operation: operation.into(),
retryable: true,
source: Some(Box::new(source)),
}
}
/// Construct a permanent storage failure carrying its source.
pub fn permanent_storage(
operation: impl Into<String>,
source: impl Error + Send + Sync + 'static,
) -> Self {
RepositoryError::Storage {
operation: operation.into(),
retryable: false,
source: Some(Box::new(source)),
}
}
/// Classify this error for retry purposes.
///
/// The contract a runner relies on: a retryable error should be redelivered
/// (a later attempt may succeed); a permanent error should not, because
/// re-running the identical operation cannot change a deterministic outcome.
///
/// - `Storage { retryable, .. }` reports the classification captured when the
/// backend error was mapped (connection/pool/timeout → retryable;
/// constraint/decode → permanent).
/// - `Lock` defers to [`LockError::kind`].
/// - `ConcurrentWrite` is **retryable**: an optimistic-concurrency conflict
/// means another writer won the race; reloading and reapplying typically
/// succeeds. This preserves the prior behavior where it fell into the
/// retryable bucket.
/// - `NotFound` is retryable: under at-least-once delivery it is usually an
/// out-of-order race a later redelivery resolves.
/// - The deterministic faults (`Model`, `Replay`, invalid identity/receipt,
/// `InvalidState`, duplicate-in-batch, `LockPoisoned`) are permanent.
pub fn kind(&self) -> RetryClass {
match self {
RepositoryError::Storage { retryable, .. } => {
if *retryable {
RetryClass::Retryable
} else {
RetryClass::Permanent
}
}
RepositoryError::Lock(err) => err.kind(),
RepositoryError::ConcurrentWrite { .. } | RepositoryError::NotFound { .. } => {
RetryClass::Retryable
}
RepositoryError::LockPoisoned(_)
| RepositoryError::DuplicateStreamInBatch { .. }
| RepositoryError::DuplicateOutboxMessageInBatch { .. }
| RepositoryError::DuplicateInboxReceipt { .. }
| RepositoryError::InvalidInboxReceipt { .. }
| RepositoryError::CausalWriteRequired { .. }
| RepositoryError::InvalidStreamIdentity { .. }
| RepositoryError::InvalidState { .. }
| RepositoryError::Replay(_)
| RepositoryError::Model(_) => RetryClass::Permanent,
}
}
/// Whether this error is retryable.
pub fn is_retryable(&self) -> bool {
self.kind().is_retryable()
}
}
impl fmt::Display for RepositoryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RepositoryError::LockPoisoned(operation) => {
write!(f, "repository lock poisoned during {}", operation)
}
RepositoryError::Lock(err) => write!(f, "repository lock error: {}", err),
RepositoryError::ConcurrentWrite {
id,
expected,
actual,
} => write!(
f,
"concurrent write detected for entity {} (expected version {}, got {})",
id, expected, actual
),
RepositoryError::DuplicateStreamInBatch { id } => {
write!(f, "duplicate stream id in commit batch: {}", id)
}
RepositoryError::DuplicateOutboxMessageInBatch { id } => {
write!(f, "duplicate outbox message id in commit batch: {}", id)
}
RepositoryError::DuplicateInboxReceipt {
consumer,
message_id,
} => write!(
f,
"consumer inbox receipt already recorded for consumer `{}`, message `{}`",
consumer, message_id
),
RepositoryError::InvalidInboxReceipt {
consumer,
message_id,
} => write!(
f,
"invalid consumer inbox receipt (consumer `{}`, message `{}`): consumer and message id must be non-empty",
consumer, message_id
),
RepositoryError::CausalWriteRequired { table } => write!(
f,
"table `{table}` is causal-owned and requires the projection commit path"
),
RepositoryError::InvalidStreamIdentity {
aggregate_type,
aggregate_id,
reason,
} => write!(
f,
"invalid stream identity (type `{}`, id `{}`): {}",
aggregate_type, aggregate_id, reason
),
RepositoryError::NotFound { id } => write!(f, "entity not found: {}", id),
RepositoryError::InvalidState {
id,
expected,
actual,
} => write!(
f,
"invalid state for entity {} (expected {}, got {})",
id, expected, actual
),
RepositoryError::Replay(message) => write!(f, "replay error: {}", message),
RepositoryError::Model(message) => write!(f, "model error: {}", message),
RepositoryError::Storage {
operation,
retryable,
source,
} => {
let class = if *retryable { "retryable" } else { "permanent" };
match source {
Some(source) => {
write!(f, "storage error ({class}) during {operation}: {source}")
}
None => write!(f, "storage error ({class}) during {operation}"),
}
}
}
}
}
impl Error for RepositoryError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
RepositoryError::Lock(err) => Some(err),
RepositoryError::Storage {
source: Some(source),
..
} => Some(source.as_ref()),
_ => None,
}
}
}
impl From<LockError> for RepositoryError {
fn from(err: LockError) -> Self {
RepositoryError::Lock(err)
}
}
impl From<TableStoreError> for RepositoryError {
fn from(err: TableStoreError) -> Self {
if let TableStoreError::CausalWriteRequired { table } = err {
return RepositoryError::CausalWriteRequired { table };
}
// Map to `Storage` so the read-model error keeps a retry signal and its
// source instead of collapsing to an opaque `Model` string. Locks and
// structured backend failures carry the retry classification through;
// every other read-model variant is deterministic (a concurrency
// conflict, serde/metadata fault, or not-found will fail the same way on
// redelivery). Legacy `TableStoreError::Storage` remains string-only and
// therefore permanent by default; guessing retryability from text would
// risk an infinite poison-message loop.
let retryable = match &err {
TableStoreError::Lock(lock) => lock.is_retryable(),
TableStoreError::BackendStorage { retryable, .. } => *retryable,
_ => false,
};
RepositoryError::Storage {
operation: "read model".into(),
retryable,
source: Some(Box::new(err)),
}
}
}
impl From<EventRecordError> for RepositoryError {
fn from(err: EventRecordError) -> Self {
// Event (de)serialization faults are deterministic: the same bytes will
// fail the same way on redelivery. Classify as permanent storage, but
// preserve the source for diagnostics rather than stringifying it away.
RepositoryError::Storage {
operation: "event record".into(),
retryable: false,
source: Some(Box::new(err)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table_backend_retry_class_survives_repository_conversion() {
let transient = RepositoryError::from(TableStoreError::BackendStorage {
operation: "sqlite projection write".into(),
retryable: true,
message: "database is busy".into(),
});
let permanent = RepositoryError::from(TableStoreError::BackendStorage {
operation: "postgres projection write".into(),
retryable: false,
message: "constraint violation".into(),
});
assert!(transient.is_retryable());
assert!(!permanent.is_retryable());
assert!(transient.to_string().contains("retryable"));
assert!(permanent.to_string().contains("permanent"));
let causal = RepositoryError::from(TableStoreError::CausalWriteRequired {
table: "todo_views".into(),
});
assert!(matches!(
causal,
RepositoryError::CausalWriteRequired { ref table } if table == "todo_views"
));
assert!(!causal.is_retryable());
}
}