1use thiserror::Error;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
7#[repr(u8)]
8pub enum DCPError {
9 #[error("insufficient data for parsing")]
11 InsufficientData = 1,
12 #[error("invalid magic number")]
14 InvalidMagic = 2,
15 #[error("unknown message type")]
17 UnknownMessageType = 3,
18 #[error("tool not found")]
20 ToolNotFound = 4,
21 #[error("schema validation failed")]
23 ValidationFailed = 5,
24 #[error("hash mismatch")]
26 HashMismatch = 6,
27 #[error("signature invalid")]
29 SignatureInvalid = 7,
30 #[error("nonce reused")]
32 NonceReused = 8,
33 #[error("timestamp expired")]
35 TimestampExpired = 9,
36 #[error("checksum mismatch")]
38 ChecksumMismatch = 10,
39 #[error("backpressure")]
41 Backpressure = 11,
42 #[error("out of bounds")]
44 OutOfBounds = 12,
45 #[error("internal error")]
47 InternalError = 13,
48 #[error("resource exhausted")]
50 ResourceExhausted = 14,
51 #[error("session not found")]
53 SessionNotFound = 15,
54 #[error("capability denied")]
56 CapabilityDenied = 16,
57}
58
59#[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#[repr(C, packed)]
81#[derive(Debug, Clone, Copy)]
82pub struct ErrorResponse {
83 pub category: u8,
85 pub code: u8,
87 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}