Skip to main content

nodedb_types/error/
code.rs

1//! Stable numeric error codes for programmatic error handling.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// Stable numeric error codes for programmatic error handling.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub struct ErrorCode(pub u16);
10
11impl ErrorCode {
12    // Write path (1000–1099)
13    pub const CONSTRAINT_VIOLATION: Self = Self(1000);
14    pub const WRITE_CONFLICT: Self = Self(1001);
15    pub const DEADLINE_EXCEEDED: Self = Self(1002);
16    pub const PREVALIDATION_REJECTED: Self = Self(1003);
17    pub const APPEND_ONLY_VIOLATION: Self = Self(1010);
18    pub const BALANCE_VIOLATION: Self = Self(1011);
19    pub const PERIOD_LOCKED: Self = Self(1012);
20    pub const STATE_TRANSITION_VIOLATION: Self = Self(1013);
21    pub const TRANSITION_CHECK_VIOLATION: Self = Self(1014);
22    pub const RETENTION_VIOLATION: Self = Self(1015);
23    pub const LEGAL_HOLD_ACTIVE: Self = Self(1016);
24    pub const TYPE_MISMATCH: Self = Self(1020);
25    pub const OVERFLOW: Self = Self(1021);
26    pub const INSUFFICIENT_BALANCE: Self = Self(1022);
27    pub const RATE_EXCEEDED: Self = Self(1023);
28    pub const TYPE_GUARD_VIOLATION: Self = Self(1024);
29
30    // Read path (1100–1199)
31    pub const COLLECTION_NOT_FOUND: Self = Self(1100);
32    pub const DOCUMENT_NOT_FOUND: Self = Self(1101);
33    pub const COLLECTION_DRAINING: Self = Self(1102);
34    pub const COLLECTION_DEACTIVATED: Self = Self(1103);
35
36    // Query (1200–1299)
37    pub const PLAN_ERROR: Self = Self(1200);
38    pub const FAN_OUT_EXCEEDED: Self = Self(1201);
39    pub const SQL_NOT_ENABLED: Self = Self(1202);
40
41    // Auth / Security (2000–2099)
42    pub const AUTHORIZATION_DENIED: Self = Self(2000);
43    pub const AUTH_EXPIRED: Self = Self(2001);
44
45    // Sync (3000–3099)
46    pub const SYNC_CONNECTION_FAILED: Self = Self(3000);
47    pub const SYNC_DELTA_REJECTED: Self = Self(3001);
48    pub const SHAPE_SUBSCRIPTION_FAILED: Self = Self(3002);
49
50    // Storage (4000–4099)
51    pub const STORAGE: Self = Self(4000);
52    pub const SEGMENT_CORRUPTED: Self = Self(4001);
53    pub const COLD_STORAGE: Self = Self(4002);
54
55    // WAL (4100–4199)
56    pub const WAL: Self = Self(4100);
57
58    // Serialization (4200–4299)
59    pub const SERIALIZATION: Self = Self(4200);
60    pub const CODEC: Self = Self(4201);
61
62    // Config (5000–5099)
63    pub const CONFIG: Self = Self(5000);
64    pub const BAD_REQUEST: Self = Self(5001);
65
66    // Cluster (6000–6099)
67    pub const NO_LEADER: Self = Self(6000);
68    pub const NOT_LEADER: Self = Self(6001);
69    pub const MIGRATION_IN_PROGRESS: Self = Self(6002);
70    pub const NODE_UNREACHABLE: Self = Self(6003);
71    pub const CLUSTER: Self = Self(6010);
72
73    // Memory (7000–7099)
74    pub const MEMORY_EXHAUSTED: Self = Self(7000);
75
76    // Encryption (8000–8099)
77    pub const ENCRYPTION: Self = Self(8000);
78
79    // Internal (9000–9099)
80    pub const INTERNAL: Self = Self(9000);
81    pub const BRIDGE: Self = Self(9001);
82    pub const DISPATCH: Self = Self(9002);
83}
84
85impl fmt::Display for ErrorCode {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "NDB-{:04}", self.0)
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn error_code_display() {
97        assert_eq!(ErrorCode::CONSTRAINT_VIOLATION.to_string(), "NDB-1000");
98        assert_eq!(ErrorCode::INTERNAL.to_string(), "NDB-9000");
99        assert_eq!(ErrorCode::WAL.to_string(), "NDB-4100");
100    }
101}