Skip to main content

nodedb_crdt/
error.rs

1// SPDX-License-Identifier: BUSL-1.1
2
3//! Error types for the CRDT engine.
4
5/// Errors produced by CRDT operations.
6#[derive(Debug, thiserror::Error)]
7pub enum CrdtError {
8    /// A constraint was violated during validation.
9    #[error("constraint violation: {constraint} on collection `{collection}`: {detail}")]
10    ConstraintViolation {
11        constraint: String,
12        collection: String,
13        detail: String,
14    },
15
16    /// The delta could not be applied to the current state.
17    #[error("delta application failed: {0}")]
18    DeltaApplyFailed(String),
19
20    /// Loro internal error.
21    #[error("loro error: {0}")]
22    Loro(String),
23
24    /// Dead-letter queue is full.
25    #[error("dead-letter queue full: capacity {capacity}, pending {pending}")]
26    DlqFull { capacity: usize, pending: usize },
27
28    /// The collection does not exist.
29    #[error("unknown collection: {0}")]
30    UnknownCollection(String),
31
32    /// Auth context has expired — agent must re-authenticate before syncing.
33    #[error("auth expired: user {user_id} must re-authenticate (expired at {expired_at})")]
34    AuthExpired { user_id: u64, expired_at: u64 },
35
36    /// Delta signature verification failed.
37    #[error("delta signature invalid for user {user_id}: {detail}")]
38    InvalidSignature { user_id: u64, detail: String },
39
40    /// Replay attack detected: seq_no already seen for this (user_id, device_id).
41    ///
42    /// The submitted `seq_no` is not strictly greater than the last accepted
43    /// sequence number, indicating a replayed or out-of-order delta.
44    #[error(
45        "replay detected for user {user_id} device {device_id}: \
46         seq_no {seq_no} <= last_seen {last_seen}"
47    )]
48    ReplayDetected {
49        user_id: u64,
50        device_id: u64,
51        seq_no: u64,
52        last_seen: u64,
53    },
54}
55
56pub type Result<T> = std::result::Result<T, CrdtError>;