Skip to main content

rtp_engine/
error.rs

1//! Error types for rtp-engine.
2
3use thiserror::Error;
4
5/// Result type alias for rtp-engine operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur in rtp-engine.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Codec initialization or encoding/decoding error.
12    #[error("codec error: {0}")]
13    Codec(String),
14
15    /// RTP packet parsing or construction error.
16    #[error("RTP error: {0}")]
17    Rtp(String),
18
19    /// RTCP packet parsing or construction error.
20    #[error("RTCP error: {0}")]
21    Rtcp(String),
22
23    /// SRTP encryption/decryption error.
24    #[error("SRTP error: {0}")]
25    Srtp(String),
26
27    /// Audio device error.
28    #[error("device error: {0}")]
29    Device(String),
30
31    /// Network I/O error.
32    #[error("network error: {0}")]
33    Network(#[from] std::io::Error),
34
35    /// Invalid configuration or parameter.
36    #[error("invalid parameter: {0}")]
37    InvalidParameter(String),
38}
39
40impl Error {
41    /// Create a codec error.
42    pub fn codec(msg: impl Into<String>) -> Self {
43        Self::Codec(msg.into())
44    }
45
46    /// Create an RTP error.
47    pub fn rtp(msg: impl Into<String>) -> Self {
48        Self::Rtp(msg.into())
49    }
50
51    /// Create an RTCP error.
52    pub fn rtcp(msg: impl Into<String>) -> Self {
53        Self::Rtcp(msg.into())
54    }
55
56    /// Create an SRTP error.
57    pub fn srtp(msg: impl Into<String>) -> Self {
58        Self::Srtp(msg.into())
59    }
60
61    /// Create a device error.
62    pub fn device(msg: impl Into<String>) -> Self {
63        Self::Device(msg.into())
64    }
65
66    /// Create an invalid parameter error.
67    pub fn invalid_parameter(msg: impl Into<String>) -> Self {
68        Self::InvalidParameter(msg.into())
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_error_constructors() {
78        let e = Error::codec("test codec error");
79        assert!(matches!(e, Error::Codec(_)));
80        assert_eq!(format!("{}", e), "codec error: test codec error");
81
82        let e = Error::rtp("test rtp error");
83        assert!(matches!(e, Error::Rtp(_)));
84        assert_eq!(format!("{}", e), "RTP error: test rtp error");
85
86        let e = Error::rtcp("test rtcp error");
87        assert!(matches!(e, Error::Rtcp(_)));
88        assert_eq!(format!("{}", e), "RTCP error: test rtcp error");
89
90        let e = Error::srtp("test srtp error");
91        assert!(matches!(e, Error::Srtp(_)));
92        assert_eq!(format!("{}", e), "SRTP error: test srtp error");
93
94        let e = Error::device("test device error");
95        assert!(matches!(e, Error::Device(_)));
96        assert_eq!(format!("{}", e), "device error: test device error");
97
98        let e = Error::invalid_parameter("bad param");
99        assert!(matches!(e, Error::InvalidParameter(_)));
100        assert_eq!(format!("{}", e), "invalid parameter: bad param");
101    }
102
103    #[test]
104    fn test_error_from_io() {
105        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
106        let e: Error = io_err.into();
107        assert!(matches!(e, Error::Network(_)));
108        assert!(format!("{}", e).contains("file not found"));
109    }
110
111    #[test]
112    fn test_error_debug() {
113        let e = Error::codec("debug test");
114        let debug_str = format!("{:?}", e);
115        assert!(debug_str.contains("Codec"));
116        assert!(debug_str.contains("debug test"));
117    }
118}