minint/
error.rs

1use rmp::decode::{bytes::BytesReadError, ValueReadError};
2use std::error::Error;
3use std::fmt::{self, Display};
4use tokio_tungstenite::tungstenite::Error as TungsteniteError;
5
6/// Custom error types for NetworkTables operations
7#[derive(Debug)]
8pub enum NtError {
9    /// Error during connection establishment
10    ConnectionError(String),
11    /// Error with websocket communication
12    WebsocketError(String),
13    /// Error with MessagePack encoding/decoding
14    MessagePackError(String),
15    /// Error with JSON serialization/deserialization
16    JsonError(String),
17    /// Error with message sending
18    SendError(String),
19    /// Generic IO error
20    IoError(String),
21    /// Topic not found
22    TopicNotFound(String),
23    /// Binary frame parsing error
24    BinaryFrameError,
25    /// Lock acquisition error
26    LockError(String),
27    /// Other errors
28    Other(String),
29    NeedReconnect,
30}
31
32impl Display for NtError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            NtError::ConnectionError(msg) => write!(f, "Connection error: {msg}"),
36            NtError::WebsocketError(msg) => write!(f, "Websocket error: {msg}"),
37            NtError::MessagePackError(msg) => write!(f, "MessagePack error: {msg}"),
38            NtError::JsonError(msg) => write!(f, "JSON error: {msg}"),
39            NtError::SendError(msg) => write!(f, "Send error: {msg}"),
40            NtError::IoError(msg) => write!(f, "IO error: {msg}"),
41            NtError::TopicNotFound(msg) => write!(f, "Topic not found: {msg}"),
42            NtError::BinaryFrameError => write!(f, "Failed to parse binary frame"),
43            NtError::LockError(msg) => write!(f, "Lock error: {msg}"),
44            NtError::Other(msg) => write!(f, "Error: {msg}"),
45            NtError::NeedReconnect => write!(f, "Disconnected from NT server! Must reconnect"),
46        }
47    }
48}
49
50impl Error for NtError {}
51
52// Implement From traits for various error types
53impl From<std::io::Error> for NtError {
54    fn from(err: std::io::Error) -> Self {
55        NtError::IoError(err.to_string())
56    }
57}
58
59impl From<TungsteniteError> for NtError {
60    fn from(err: TungsteniteError) -> Self {
61        NtError::WebsocketError(err.to_string())
62    }
63}
64
65impl From<serde_json::Error> for NtError {
66    fn from(err: serde_json::Error) -> Self {
67        NtError::JsonError(err.to_string())
68    }
69}
70
71impl From<rmp::encode::ValueWriteError> for NtError {
72    fn from(err: rmp::encode::ValueWriteError) -> Self {
73        NtError::MessagePackError(err.to_string())
74    }
75}
76
77impl From<rmp::decode::ValueReadError> for NtError {
78    fn from(err: rmp::decode::ValueReadError) -> Self {
79        NtError::MessagePackError(format!("{:?}", err))
80    }
81}
82
83impl From<ValueReadError<BytesReadError>> for NtError {
84    fn from(err: ValueReadError<BytesReadError>) -> Self {
85        NtError::MessagePackError(format!("{:?}", err))
86    }
87}
88
89impl From<tokio_tungstenite::tungstenite::http::Error> for NtError {
90    fn from(err: tokio_tungstenite::tungstenite::http::Error) -> Self {
91        NtError::ConnectionError(err.to_string())
92    }
93}
94
95// Type alias for Result with NtError
96pub type Result<T> = std::result::Result<T, NtError>;