rvoip_rtp_core/
error.rs

1use thiserror::Error;
2use std::io;
3
4/// Error type for RTP operations
5#[derive(Debug, Error, Clone)]
6pub enum Error {
7    /// Error when encoding RTP packet
8    #[error("Failed to encode RTP packet: {0}")]
9    EncodeError(String),
10
11    /// Error when decoding RTP packet
12    #[error("Failed to decode RTP packet: {0}")]
13    DecodeError(String),
14
15    /// Invalid packet format
16    #[error("Invalid RTP packet format: {0}")]
17    InvalidPacket(String),
18
19    /// Buffer too small
20    #[error("Buffer too small for RTP packet: need {required} but have {available}")]
21    BufferTooSmall {
22        required: usize,
23        available: usize,
24    },
25
26    /// Invalid parameter for RTP operation
27    #[error("Invalid parameter: {0}")]
28    InvalidParameter(String),
29
30    /// IO error when sending/receiving RTP packets
31    #[error("IO error: {0}")]
32    IoError(String),
33
34    /// RTCP error
35    #[error("RTCP error: {0}")]
36    RtcpError(String),
37
38    /// Session error
39    #[error("RTP session error: {0}")]
40    SessionError(String),
41    
42    /// Transport error
43    #[error("Transport error: {0}")]
44    Transport(String),
45    
46    /// Parsing error
47    #[error("Parse error: {0}")]
48    ParseError(String),
49    
50    /// SRTP error
51    #[error("SRTP error: {0}")]
52    SrtpError(String),
53    
54    /// Statistics error
55    #[error("Statistics error: {0}")]
56    StatsError(String),
57    
58    /// Timing error
59    #[error("Timing error: {0}")]
60    TimingError(String),
61
62    /// Invalid protocol version
63    #[error("Invalid protocol version: {0}")]
64    InvalidProtocolVersion(String),
65
66    /// Unsupported feature
67    #[error("Unsupported feature: {0}")]
68    UnsupportedFeature(String),
69    
70    /// Not implemented yet
71    #[error("Not implemented: {0}")]
72    NotImplemented(String),
73    
74    /// Invalid state for operation
75    #[error("Invalid state: {0}")]
76    InvalidState(String),
77    
78    /// DTLS handshake error
79    #[error("DTLS handshake error: {0}")]
80    DtlsHandshakeError(String),
81    
82    /// DTLS alert received
83    #[error("DTLS alert received: {0}")]
84    DtlsAlertReceived(String),
85    
86    /// Certificate validation error
87    #[error("Certificate validation error: {0}")]
88    CertificateValidationError(String),
89    
90    /// Cryptographic operation error
91    #[error("Cryptographic error: {0}")]
92    CryptoError(String),
93    
94    /// Invalid message format or content
95    #[error("Invalid message: {0}")]
96    InvalidMessage(String),
97    
98    /// Authentication failed
99    #[error("Authentication failed: {0}")]
100    AuthenticationFailed(String),
101    
102    /// Negotiation failed
103    #[error("Negotiation failed: {0}")]
104    NegotiationFailed(String),
105    
106    /// Packet too short
107    #[error("Packet too short")]
108    PacketTooShort,
109    
110    /// Timeout error
111    #[error("Operation timed out: {0}")]
112    Timeout(String),
113
114    /// Serialization error
115    #[error("Serialization error: {0}")]
116    SerializationError(String),
117}
118
119impl From<io::Error> for Error {
120    fn from(err: io::Error) -> Self {
121        Error::IoError(err.to_string())
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128    
129    #[test]
130    fn test_error_display() {
131        let encode_err = Error::EncodeError("test error".to_string());
132        assert_eq!(encode_err.to_string(), "Failed to encode RTP packet: test error");
133        
134        let buffer_err = Error::BufferTooSmall { required: 100, available: 50 };
135        assert_eq!(buffer_err.to_string(), "Buffer too small for RTP packet: need 100 but have 50");
136        
137        let io_err = Error::from(io::Error::new(io::ErrorKind::NotFound, "file not found"));
138        assert!(io_err.to_string().contains("IO error"));
139    }
140}