Skip to main content

motorcortex_rust/
error.rs

1use crate::msg::StatusCode;
2use std::fmt;
3
4/// Error type for all motorcortex-rust operations.
5#[derive(Debug)]
6pub enum MotorcortexError {
7    /// Connection failed (TLS, socket, timeout)
8    Connection(String),
9
10    /// Message encoding failed
11    Encode(String),
12
13    /// Message decoding failed
14    Decode(String),
15
16    /// Parameter path not found in the tree
17    ParameterNotFound(String),
18
19    /// Server returned a non-OK status
20    Status(StatusCode),
21
22    /// NNG-level I/O failure (send/receive)
23    Io(String),
24
25    /// Subscription operation failed
26    Subscription(String),
27}
28
29impl fmt::Display for MotorcortexError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::Connection(msg) => write!(f, "Connection error: {}", msg),
33            Self::Encode(msg) => write!(f, "Encode error: {}", msg),
34            Self::Decode(msg) => write!(f, "Decode error: {}", msg),
35            Self::ParameterNotFound(path) => write!(f, "Parameter not found: {}", path),
36            Self::Status(code) => write!(f, "Server status: {:?}", code),
37            Self::Io(msg) => write!(f, "I/O error: {}", msg),
38            Self::Subscription(msg) => write!(f, "Subscription error: {}", msg),
39        }
40    }
41}
42
43impl std::error::Error for MotorcortexError {}
44
45impl From<prost::DecodeError> for MotorcortexError {
46    fn from(e: prost::DecodeError) -> Self {
47        Self::Decode(e.to_string())
48    }
49}
50
51/// Convenience alias used throughout the library.
52pub type Result<T> = std::result::Result<T, MotorcortexError>;