rust_ethernet_ip/
error.rs

1// use std::error::Error;
2use std::io;
3use std::time::Duration;
4use thiserror::Error;
5
6/// Result type alias for EtherNet/IP operations
7pub type Result<T> = std::result::Result<T, EtherNetIpError>;
8
9/// Error types that can occur during EtherNet/IP communication
10#[derive(Debug, Error)]
11pub enum EtherNetIpError {
12    /// IO error (network issues, connection problems)
13    #[error("IO error: {0}")]
14    Io(#[from] io::Error),
15
16    /// Protocol error (invalid packet format, unsupported features)
17    #[error("Protocol error: {0}")]
18    Protocol(String),
19
20    /// Tag not found in PLC
21    #[error("Tag not found: {0}")]
22    TagNotFound(String),
23
24    /// Data type mismatch
25    #[error("Data type mismatch: expected {expected}, got {actual}")]
26    DataTypeMismatch { expected: String, actual: String },
27
28    /// Write error with status code
29    #[error("Write error: {message} (status: {status})")]
30    WriteError { status: u8, message: String },
31
32    /// Read error with status code
33    #[error("Read error: {message} (status: {status})")]
34    ReadError { status: u8, message: String },
35
36    /// Invalid response from PLC
37    #[error("Invalid response: {reason}")]
38    InvalidResponse { reason: String },
39
40    /// Timeout error
41    #[error("Operation timed out after {0:?}")]
42    Timeout(Duration),
43
44    /// UDT error
45    #[error("UDT error: {0}")]
46    Udt(String),
47
48    /// Connection error (PLC not responding, session issues)
49    #[error("Connection error: {0}")]
50    Connection(String),
51
52    /// String is too long for the PLC's string type
53    #[error("String too long: max length is {max_length}, but got {actual_length}")]
54    StringTooLong {
55        max_length: usize,
56        actual_length: usize,
57    },
58
59    /// String contains invalid characters
60    #[error("Invalid string: {reason}")]
61    InvalidString { reason: String },
62
63    /// String write operation failed
64    #[error("String write failed: {message} (status: {status})")]
65    StringWriteError { status: u8, message: String },
66
67    /// String read operation failed
68    #[error("String read failed: {message} (status: {status})")]
69    StringReadError { status: u8, message: String },
70
71    /// Invalid string response from PLC
72    #[error("Invalid string response: {reason}")]
73    InvalidStringResponse { reason: String },
74
75    /// Tag error
76    #[error("Tag error: {0}")]
77    Tag(String),
78
79    /// Permission denied
80    #[error("Permission denied: {0}")]
81    Permission(String),
82
83    /// UTF-8 error
84    #[error("UTF-8 error: {0}")]
85    Utf8(#[from] std::string::FromUtf8Error),
86
87    /// Other error
88    #[error("Other error: {0}")]
89    Other(String),
90
91    /// Subscription error
92    #[error("Subscription error: {0}")]
93    Subscription(String),
94}