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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Engine-level error types.
/// Errors that can occur during engine operations (command dispatch, event
/// persistence, state reconstruction).
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum EngineError {
/// The event store returned an error.
#[error("store error: {message}")]
Store {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// Optimistic concurrency check failed: the stream was modified by a
/// concurrent writer between the read and the append.
#[error("version conflict: expected {expected}, found {actual}")]
VersionConflict {
/// The sequence number the caller expected the stream to be at.
expected: u64,
/// The actual current sequence number of the stream.
actual: u64,
},
/// Could not deserialize a stored event payload into the typed event.
///
/// This typically indicates a schema migration is required.
#[error("event deserialization failed: {0}")]
Deserialization(String),
/// Could not serialize a domain event into the envelope payload.
#[error("event serialization failed: {0}")]
Serialization(String),
/// A snapshot storage operation failed.
#[error("snapshot store error: {message}")]
Snapshot {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// An outbox storage operation failed.
#[error("outbox store error: {message}")]
Outbox {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// A deadline storage operation failed.
#[error("deadline store error: {message}")]
Deadline {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// A process registry operation failed.
#[error("process registry error: {message}")]
Registry {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// An inbox (AS4 dedup) operation failed.
#[error("inbox store error: {message}")]
Inbox {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// A partner-store operation failed, or a required partner record is absent.
#[error("partner store error: {message}")]
Partner {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// A dead-letter query operation failed.
///
/// Covers `SlateDbDeadLetterSink::list_recent` and similar read-path
/// operations. Writes are fire-and-forget (logged on error) and do not
/// produce this variant.
#[error("dead-letter store error: {message}")]
DeadLetter {
/// Human-readable description of the storage failure.
message: String,
/// `true` when the error is transient (retry may succeed after backoff).
transient: bool,
},
/// The workflow rejected the command or reached an invalid state.
#[error("workflow error: {0}")]
Workflow(#[from] WorkflowError),
/// Appending the requested events would exceed the per-stream event count
/// limit configured on the store.
///
/// This is a hard safety guard against runaway streams. The caller should
/// archive or compact the stream before retrying.
///
/// The `stream_id`, `limit`, `new_events`, and `actual` fields are
/// available for internal structured logging but are intentionally **not**
/// included in the `Display` string returned to API callers to avoid
/// leaking internal stream topology.
#[error("event stream quota exceeded")]
StreamQuotaExceeded {
/// The stream that hit the limit (for internal logging only).
stream_id: crate::ids::StreamId,
/// The configured maximum number of events per stream.
limit: u64,
/// Number of events that would be written by this append.
new_events: usize,
/// Total event count after the append would complete.
actual: u64,
},
/// An AS4 transport send operation failed.
///
/// Distinct from [`Store`] so the outbox worker can decide retry strategy
/// without string-matching: transport errors are potentially
/// transient; serialization errors are permanent.
///
/// [`Store`]: EngineError::Store
#[error("AS4 transport error sending to {endpoint}: {message}")]
Transport {
/// The AS4 endpoint URL (or `"unknown"` when not available).
endpoint: Box<str>,
/// The underlying error description.
message: String,
},
/// The outbound message cannot be delivered because no AS4 endpoint is
/// registered for the recipient GLN.
///
/// This is a **permanent** failure: the operator must add the missing
/// `--as4-partner <GLN>=<URL>` entry before delivery can succeed.
/// The outbox worker should dead-letter immediately rather than retrying.
#[error("no AS4 endpoint configured for recipient {recipient}")]
PartnerUnknown {
/// The recipient GLN that has no registered endpoint.
recipient: Box<str>,
},
/// The outbound message cannot be rendered to EDIFACT wire format because
/// no renderer is implemented for its message type.
///
/// This is a **permanent** failure: retrying will never succeed until a
/// wire-format renderer is implemented for the message type. The outbox
/// worker should dead-letter the message immediately and alert the operator.
///
/// Use this instead of silently transmitting JSON blobs over AS4, which
/// violates BDEW MaKo interoperability requirements.
#[error(
"no EDIFACT renderer implemented for message type '{message_type}' \
(outbox entry {message_id}); implement a wire-format renderer before \
enabling this workflow path in production"
)]
RendererNotImplemented {
/// The EDIFACT message type string (e.g. `"MSCONS"`, `"INVOIC"`).
message_type: Box<str>,
/// The outbox message ID for correlation with the dead-letter store.
message_id: Box<str>,
},
/// A string could not be converted into a valid [`StreamId`].
///
/// Stream IDs must be non-empty and must not contain NUL bytes.
/// This error is produced by [`StreamId::try_new`] and the
/// [`TryFrom`] impls. Use the typed constructors
/// ([`StreamId::for_process`], [`StreamId::for_partner`]) where possible
/// to avoid constructing stream IDs from raw strings.
///
/// [`StreamId`]: crate::ids::StreamId
/// [`StreamId::try_new`]: crate::ids::StreamId::try_new
/// [`StreamId::for_process`]: crate::ids::StreamId::for_process
/// [`StreamId::for_partner`]: crate::ids::StreamId::for_partner
#[error("invalid stream ID: {reason}")]
InvalidStreamId {
/// The rejected input (truncated to 200 chars for log safety).
input: Box<str>,
/// Human-readable explanation of why the ID was rejected.
reason: &'static str,
},
}
impl EngineError {
// ── Storage error constructors ────────────────────────────────────────────
/// Construct a **permanent** (non-retriable) event-store error.
pub fn store(message: impl Into<String>) -> Self {
Self::Store {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** (retriable) event-store error.
pub fn transient_store(message: impl Into<String>) -> Self {
Self::Store {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** outbox-store error.
pub fn outbox(message: impl Into<String>) -> Self {
Self::Outbox {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** outbox-store error.
pub fn transient_outbox(message: impl Into<String>) -> Self {
Self::Outbox {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** deadline-store error.
pub fn deadline(message: impl Into<String>) -> Self {
Self::Deadline {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** deadline-store error.
pub fn transient_deadline(message: impl Into<String>) -> Self {
Self::Deadline {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** process-registry error.
pub fn registry(message: impl Into<String>) -> Self {
Self::Registry {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** process-registry error.
pub fn transient_registry(message: impl Into<String>) -> Self {
Self::Registry {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** inbox-store error.
pub fn inbox(message: impl Into<String>) -> Self {
Self::Inbox {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** inbox-store error.
pub fn transient_inbox(message: impl Into<String>) -> Self {
Self::Inbox {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** snapshot-store error.
pub fn snapshot(message: impl Into<String>) -> Self {
Self::Snapshot {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** snapshot-store error.
pub fn transient_snapshot(message: impl Into<String>) -> Self {
Self::Snapshot {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** partner-store error.
pub fn partner(message: impl Into<String>) -> Self {
Self::Partner {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** partner-store error.
pub fn transient_partner(message: impl Into<String>) -> Self {
Self::Partner {
message: message.into(),
transient: true,
}
}
/// Construct a **permanent** dead-letter-store error.
pub fn dead_letter(message: impl Into<String>) -> Self {
Self::DeadLetter {
message: message.into(),
transient: false,
}
}
/// Construct a **transient** dead-letter-store error.
pub fn transient_dead_letter(message: impl Into<String>) -> Self {
Self::DeadLetter {
message: message.into(),
transient: true,
}
}
// ── Predicate helpers ─────────────────────────────────────────────────────
/// Return `true` when this is a [`EngineError::VersionConflict`].
///
/// Useful for retry logic: on a version conflict the caller should reload
/// state and re-issue the command.
#[must_use]
pub fn is_version_conflict(&self) -> bool {
matches!(self, Self::VersionConflict { .. })
}
/// Return `true` when this is a [`EngineError::StreamQuotaExceeded`].
#[must_use]
pub fn is_stream_quota_exceeded(&self) -> bool {
matches!(self, Self::StreamQuotaExceeded { .. })
}
/// Return `true` when this is a [`EngineError::Workflow`].
///
/// Useful for distinguishing infrastructure failures (store errors) from
/// domain rejections (the workflow refused the command).
#[must_use]
pub fn is_workflow_error(&self) -> bool {
matches!(self, Self::Workflow(_))
}
/// Return `true` when the error is likely transient and the operation
/// can be safely retried after a short backoff.
///
/// Storage errors carry an explicit `transient` flag set at the point of
/// construction by the storage layer, eliminating any reliance on
/// string-matching heuristics.
///
/// Transport errors (network timeouts, TLS failures) are always transient.
/// All other errors (version conflicts, quota exceeded, workflow
/// rejections, …) are permanent.
///
/// # Usage
///
/// ```rust,ignore
/// for attempt in 0..MAX_RETRIES {
/// match process.execute(cmd.clone()).await {
/// Ok(result) => return Ok(result),
/// Err(e) if e.is_version_conflict() => { /* reload and retry */ }
/// Err(e) if e.is_transient() => {
/// tokio::time::sleep(backoff(attempt)).await;
/// }
/// Err(e) => return Err(e),
/// }
/// }
/// ```
#[must_use]
pub fn is_transient(&self) -> bool {
match self {
Self::Store { transient, .. }
| Self::Outbox { transient, .. }
| Self::Deadline { transient, .. }
| Self::Registry { transient, .. }
| Self::Inbox { transient, .. }
| Self::Snapshot { transient, .. }
| Self::Partner { transient, .. }
| Self::DeadLetter { transient, .. } => *transient,
// Transport errors (network timeouts, TLS failures) are transient.
Self::Transport { .. } => true,
// Everything else (missing partner, version conflict, …) is permanent.
_ => false,
}
}
/// Return `true` when this is a [`EngineError::PartnerUnknown`].
///
/// `PartnerUnknown` is a **permanent** failure: no retry will succeed until
/// the operator registers the missing `--as4-partner` entry. The outbox
/// worker should dead-letter the message immediately.
#[must_use]
pub fn is_partner_unknown(&self) -> bool {
matches!(self, Self::PartnerUnknown { .. })
}
/// Return `true` when this is a [`EngineError::RendererNotImplemented`].
///
/// `RendererNotImplemented` is a **permanent** failure: no retry will
/// succeed until a wire-format renderer is implemented for the message type.
/// The outbox worker should dead-letter the message immediately.
#[must_use]
pub fn is_renderer_not_implemented(&self) -> bool {
matches!(self, Self::RendererNotImplemented { .. })
}
/// Return `true` when this is a [`EngineError::Transport`].
#[must_use]
pub fn is_transport_error(&self) -> bool {
matches!(self, Self::Transport { .. })
}
/// Return the inner [`WorkflowError`] if this is a workflow rejection,
/// or `None` otherwise.
#[must_use]
pub fn as_workflow_error(&self) -> Option<&WorkflowError> {
if let Self::Workflow(e) = self {
Some(e)
} else {
None
}
}
/// Return `true` when this is a [`EngineError::Snapshot`].
#[must_use]
pub fn is_snapshot_error(&self) -> bool {
matches!(self, Self::Snapshot { .. })
}
/// Return `true` when this is a [`EngineError::Outbox`].
#[must_use]
pub fn is_outbox_error(&self) -> bool {
matches!(self, Self::Outbox { .. })
}
/// Return `true` when this is a [`EngineError::Deadline`].
#[must_use]
pub fn is_deadline_error(&self) -> bool {
matches!(self, Self::Deadline { .. })
}
/// Return `true` when this is a [`EngineError::Registry`].
#[must_use]
pub fn is_registry_error(&self) -> bool {
matches!(self, Self::Registry { .. })
}
/// Return `true` when this is a [`EngineError::Inbox`].
#[must_use]
pub fn is_inbox_error(&self) -> bool {
matches!(self, Self::Inbox { .. })
}
}
/// Reasons a workflow may refuse a command.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum WorkflowError {
/// The command is not valid for the process in its current state.
#[error("command rejected: {reason}")]
CommandRejected {
/// Human-readable rejection reason.
reason: String,
},
/// The command arrived when the process was in an unexpected state.
#[error("invalid state: expected {expected}, found {actual}")]
InvalidState {
/// The state the workflow expected.
expected: String,
/// The actual state the process was in.
actual: String,
},
/// Domain validation of the command payload failed.
#[error("validation failed: {message}")]
ValidationFailed {
/// Description of what failed validation.
message: String,
},
/// The process identified by `pid` is registered in the PID router but has
/// no workflow implementation yet.
///
/// Callers should respond to the sender with a CONTRL/APERAK rejection.
/// This variant is **never** a transient error — it indicates missing
/// implementation and must not be retried.
#[error("workflow not implemented for PID {pid}")]
NotImplemented {
/// The Prüfidentifikator that has no implementation.
pid: u32,
},
/// An unexpected error occurred inside the workflow handler.
#[error("{message}")]
Other {
/// Error description.
message: String,
},
}
impl WorkflowError {
/// Construct a [`WorkflowError::CommandRejected`] with a formatted reason.
#[must_use]
pub fn rejected(reason: impl Into<String>) -> Self {
Self::CommandRejected {
reason: reason.into(),
}
}
/// Construct a [`WorkflowError::InvalidState`].
#[must_use]
pub fn invalid_state(expected: impl Into<String>, actual: impl Into<String>) -> Self {
Self::InvalidState {
expected: expected.into(),
actual: actual.into(),
}
}
/// Construct a [`WorkflowError::ValidationFailed`].
#[must_use]
pub fn validation(message: impl Into<String>) -> Self {
Self::ValidationFailed {
message: message.into(),
}
}
/// Construct a [`WorkflowError::NotImplemented`] for a given PID.
///
/// Use this to signal that the PID is routed but has no workflow
/// implementation. Callers must respond with a CONTRL/APERAK rejection
/// to the sender — this variant must never be silently discarded.
#[must_use]
pub fn not_implemented(pid: u32) -> Self {
Self::NotImplemented { pid }
}
/// Construct a [`WorkflowError::Other`].
#[must_use]
pub fn other(message: impl Into<String>) -> Self {
Self::Other {
message: message.into(),
}
}
/// Return `true` when this is a [`WorkflowError::CommandRejected`].
#[must_use]
pub fn is_rejected(&self) -> bool {
matches!(self, Self::CommandRejected { .. })
}
/// Return `true` when this is a [`WorkflowError::InvalidState`].
#[must_use]
pub fn is_invalid_state(&self) -> bool {
matches!(self, Self::InvalidState { .. })
}
/// Return `true` when this is a [`WorkflowError::ValidationFailed`].
#[must_use]
pub fn is_validation_failed(&self) -> bool {
matches!(self, Self::ValidationFailed { .. })
}
/// Return `true` when this is a [`WorkflowError::NotImplemented`].
///
/// This variant is never transient — callers must not retry.
#[must_use]
pub fn is_not_implemented(&self) -> bool {
matches!(self, Self::NotImplemented { .. })
}
}