turboclaude_protocol/
error.rs1use std::fmt;
6
7pub type Result<T> = std::result::Result<T, ProtocolError>;
9
10#[derive(Debug, Clone)]
12pub enum ProtocolError {
13 SerializationError(String),
15
16 InvalidMessage(String),
18
19 MissingField(String),
21
22 InvalidContentBlock(String),
24
25 VersionMismatch {
27 expected: u32,
29 got: u32,
31 },
32
33 InvalidControlRequest(String),
35
36 PermissionDenied(String),
38
39 Other(String),
41}
42
43impl fmt::Display for ProtocolError {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 match self {
46 Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
47 Self::InvalidMessage(msg) => write!(f, "Invalid message: {}", msg),
48 Self::MissingField(field) => write!(f, "Missing required field: {}", field),
49 Self::InvalidContentBlock(msg) => write!(f, "Invalid content block: {}", msg),
50 Self::VersionMismatch { expected, got } => {
51 write!(f, "Version mismatch: expected {}, got {}", expected, got)
52 }
53 Self::InvalidControlRequest(msg) => write!(f, "Invalid control request: {}", msg),
54 Self::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
55 Self::Other(msg) => write!(f, "{}", msg),
56 }
57 }
58}
59
60impl std::error::Error for ProtocolError {}
61
62impl From<serde_json::Error> for ProtocolError {
63 fn from(err: serde_json::Error) -> Self {
64 Self::SerializationError(err.to_string())
65 }
66}