Skip to main content

dcp/
error.rs

1//! Error types for DCP protocol.
2
3use thiserror::Error;
4
5/// DCP error types
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
7#[repr(u8)]
8pub enum DCPError {
9    /// Insufficient data for parsing
10    #[error("insufficient data for parsing")]
11    InsufficientData = 1,
12    /// Invalid magic number
13    #[error("invalid magic number")]
14    InvalidMagic = 2,
15    /// Unknown message type
16    #[error("unknown message type")]
17    UnknownMessageType = 3,
18    /// Tool not found
19    #[error("tool not found")]
20    ToolNotFound = 4,
21    /// Schema validation failed
22    #[error("schema validation failed")]
23    ValidationFailed = 5,
24    /// Hash mismatch in delta sync
25    #[error("hash mismatch")]
26    HashMismatch = 6,
27    /// Signature verification failed
28    #[error("signature invalid")]
29    SignatureInvalid = 7,
30    /// Nonce reused (replay attack)
31    #[error("nonce reused")]
32    NonceReused = 8,
33    /// Timestamp expired
34    #[error("timestamp expired")]
35    TimestampExpired = 9,
36    /// Checksum mismatch
37    #[error("checksum mismatch")]
38    ChecksumMismatch = 10,
39    /// Backpressure - consumer too slow
40    #[error("backpressure")]
41    Backpressure = 11,
42    /// Memory bounds violation
43    #[error("out of bounds")]
44    OutOfBounds = 12,
45    /// Internal error
46    #[error("internal error")]
47    InternalError = 13,
48    /// Resource exhausted
49    #[error("resource exhausted")]
50    ResourceExhausted = 14,
51    /// Session not found
52    #[error("session not found")]
53    SessionNotFound = 15,
54    /// Capability denied
55    #[error("capability denied")]
56    CapabilityDenied = 16,
57}
58
59/// Security-specific errors
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
61#[repr(u8)]
62pub enum SecurityError {
63    #[error("invalid signature")]
64    InvalidSignature = 1,
65    #[error("expired timestamp")]
66    ExpiredTimestamp = 2,
67    #[error("replay attack detected")]
68    ReplayAttack = 3,
69    #[error("insufficient capabilities")]
70    InsufficientCapabilities = 4,
71    #[error("security state capacity exceeded")]
72    CapacityExceeded = 5,
73    #[error("validation failed")]
74    ValidationFailed = 6,
75    #[error("argument hash mismatch")]
76    ArgsHashMismatch = 7,
77}
78
79/// Binary error response
80#[repr(C, packed)]
81#[derive(Debug, Clone, Copy)]
82pub struct ErrorResponse {
83    /// Error category (1 byte)
84    pub category: u8,
85    /// Error code within category (1 byte)
86    pub code: u8,
87    /// Additional context length (2 bytes)
88    pub context_len: u16,
89}
90
91impl ErrorResponse {
92    pub const SIZE: usize = 4;
93
94    pub fn new(category: u8, code: u8) -> Self {
95        Self {
96            category,
97            code,
98            context_len: 0,
99        }
100    }
101
102    pub fn from_dcp_error(err: DCPError) -> Self {
103        Self::new(1, err as u8)
104    }
105
106    pub fn from_security_error(err: SecurityError) -> Self {
107        Self::new(2, err as u8)
108    }
109}