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 {
27        expected: String,
28        actual: String,
29    },
30
31    /// Write error with status code
32    #[error("Write error: {message} (status: {status})")]
33    WriteError {
34        status: u8,
35        message: String,
36    },
37
38    /// Read error with status code
39    #[error("Read error: {message} (status: {status})")]
40    ReadError {
41        status: u8,
42        message: String,
43    },
44
45    /// Invalid response from PLC
46    #[error("Invalid response: {reason}")]
47    InvalidResponse {
48        reason: String,
49    },
50
51    /// Timeout error
52    #[error("Operation timed out after {0:?}")]
53    Timeout(Duration),
54
55    /// UDT error
56    #[error("UDT error: {0}")]
57    Udt(String),
58
59    /// Connection error (PLC not responding, session issues)
60    #[error("Connection error: {0}")]
61    Connection(String),
62
63    /// String is too long for the PLC's string type
64    #[error("String too long: max length is {max_length}, but got {actual_length}")]
65    StringTooLong {
66        max_length: usize,
67        actual_length: usize,
68    },
69
70    /// String contains invalid characters
71    #[error("Invalid string: {reason}")]
72    InvalidString {
73        reason: String,
74    },
75
76    /// String write operation failed
77    #[error("String write failed: {message} (status: {status})")]
78    StringWriteError {
79        status: u8,
80        message: String,
81    },
82
83    /// String read operation failed
84    #[error("String read failed: {message} (status: {status})")]
85    StringReadError {
86        status: u8,
87        message: String,
88    },
89
90    /// Invalid string response from PLC
91    #[error("Invalid string response: {reason}")]
92    InvalidStringResponse {
93        reason: String,
94    },
95
96    /// Tag error
97    #[error("Tag error: {0}")]
98    Tag(String),
99
100    /// Permission denied
101    #[error("Permission denied: {0}")]
102    Permission(String),
103
104    /// UTF-8 error
105    #[error("UTF-8 error: {0}")]
106    Utf8(#[from] std::string::FromUtf8Error),
107
108    /// Other error
109    #[error("Other error: {0}")]
110    Other(String),
111
112    /// Subscription error
113    #[error("Subscription error: {0}")]
114    Subscription(String),
115}