rtmp_rs/
error.rs

1//! Unified error types for rtmp-rs
2
3use std::fmt;
4use std::io;
5
6/// Result type alias using the library's Error type
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Unified error type for all RTMP operations
10#[derive(Debug)]
11pub enum Error {
12    /// I/O error during network operations
13    Io(io::Error),
14    /// RTMP protocol violation
15    Protocol(ProtocolError),
16    /// AMF encoding/decoding error
17    Amf(AmfError),
18    /// Handshake failure
19    Handshake(HandshakeError),
20    /// Media parsing error
21    Media(MediaError),
22    /// Connection rejected by peer or handler
23    Rejected(String),
24    /// Operation timed out
25    Timeout,
26    /// Connection was closed
27    ConnectionClosed,
28    /// Invalid configuration
29    Config(String),
30}
31
32impl fmt::Display for Error {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Error::Io(e) => write!(f, "I/O error: {}", e),
36            Error::Protocol(e) => write!(f, "Protocol error: {}", e),
37            Error::Amf(e) => write!(f, "AMF error: {}", e),
38            Error::Handshake(e) => write!(f, "Handshake error: {}", e),
39            Error::Media(e) => write!(f, "Media error: {}", e),
40            Error::Rejected(msg) => write!(f, "Connection rejected: {}", msg),
41            Error::Timeout => write!(f, "Operation timed out"),
42            Error::ConnectionClosed => write!(f, "Connection closed"),
43            Error::Config(msg) => write!(f, "Configuration error: {}", msg),
44        }
45    }
46}
47
48impl std::error::Error for Error {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            Error::Io(e) => Some(e),
52            _ => None,
53        }
54    }
55}
56
57impl From<io::Error> for Error {
58    fn from(err: io::Error) -> Self {
59        Error::Io(err)
60    }
61}
62
63impl From<ProtocolError> for Error {
64    fn from(err: ProtocolError) -> Self {
65        Error::Protocol(err)
66    }
67}
68
69impl From<AmfError> for Error {
70    fn from(err: AmfError) -> Self {
71        Error::Amf(err)
72    }
73}
74
75impl From<HandshakeError> for Error {
76    fn from(err: HandshakeError) -> Self {
77        Error::Handshake(err)
78    }
79}
80
81impl From<MediaError> for Error {
82    fn from(err: MediaError) -> Self {
83        Error::Media(err)
84    }
85}
86
87/// Protocol-level errors
88#[derive(Debug)]
89pub enum ProtocolError {
90    InvalidChunkHeader,
91    UnknownMessageType(u8),
92    MessageTooLarge { size: u32, max: u32 },
93    InvalidChunkStreamId(u32),
94    UnexpectedMessage(String),
95    MissingField(String),
96    InvalidCommand(String),
97    StreamNotFound(u32),
98}
99
100impl fmt::Display for ProtocolError {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        match self {
103            ProtocolError::InvalidChunkHeader => write!(f, "Invalid chunk header"),
104            ProtocolError::UnknownMessageType(t) => write!(f, "Unknown message type: {}", t),
105            ProtocolError::MessageTooLarge { size, max } => {
106                write!(f, "Message too large: {} bytes (max {})", size, max)
107            }
108            ProtocolError::InvalidChunkStreamId(id) => write!(f, "Invalid chunk stream ID: {}", id),
109            ProtocolError::UnexpectedMessage(msg) => write!(f, "Unexpected message: {}", msg),
110            ProtocolError::MissingField(field) => write!(f, "Missing required field: {}", field),
111            ProtocolError::InvalidCommand(cmd) => write!(f, "Invalid command: {}", cmd),
112            ProtocolError::StreamNotFound(id) => write!(f, "Stream not found: {}", id),
113        }
114    }
115}
116
117impl std::error::Error for ProtocolError {}
118
119/// AMF encoding/decoding errors
120#[derive(Debug)]
121pub enum AmfError {
122    UnknownMarker(u8),
123    UnexpectedEof,
124    InvalidUtf8,
125    InvalidReference(u16),
126    NestingTooDeep,
127    InvalidObjectEnd,
128}
129
130impl fmt::Display for AmfError {
131    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132        match self {
133            AmfError::UnknownMarker(m) => write!(f, "Unknown AMF marker: 0x{:02x}", m),
134            AmfError::UnexpectedEof => write!(f, "Unexpected end of AMF data"),
135            AmfError::InvalidUtf8 => write!(f, "Invalid UTF-8 in AMF string"),
136            AmfError::InvalidReference(idx) => write!(f, "Invalid AMF reference: {}", idx),
137            AmfError::NestingTooDeep => write!(f, "AMF nesting too deep"),
138            AmfError::InvalidObjectEnd => write!(f, "Invalid object end marker"),
139        }
140    }
141}
142
143impl std::error::Error for AmfError {}
144
145/// Handshake-specific errors
146#[derive(Debug)]
147pub enum HandshakeError {
148    InvalidVersion(u8),
149    DigestMismatch,
150    InvalidState,
151    ResponseMismatch,
152}
153
154impl fmt::Display for HandshakeError {
155    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156        match self {
157            HandshakeError::InvalidVersion(v) => write!(f, "Invalid RTMP version: {}", v),
158            HandshakeError::DigestMismatch => write!(f, "Handshake digest mismatch"),
159            HandshakeError::InvalidState => write!(f, "Invalid handshake state"),
160            HandshakeError::ResponseMismatch => write!(f, "Handshake response mismatch"),
161        }
162    }
163}
164
165impl std::error::Error for HandshakeError {}
166
167/// Media parsing errors
168#[derive(Debug)]
169pub enum MediaError {
170    InvalidFlvTag,
171    InvalidAvcPacket,
172    InvalidAacPacket,
173    UnsupportedCodec(String),
174    InvalidNalu,
175    MissingSequenceHeader,
176}
177
178impl fmt::Display for MediaError {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        match self {
181            MediaError::InvalidFlvTag => write!(f, "Invalid FLV tag"),
182            MediaError::InvalidAvcPacket => write!(f, "Invalid AVC packet"),
183            MediaError::InvalidAacPacket => write!(f, "Invalid AAC packet"),
184            MediaError::UnsupportedCodec(c) => write!(f, "Unsupported codec: {}", c),
185            MediaError::InvalidNalu => write!(f, "Invalid NAL unit"),
186            MediaError::MissingSequenceHeader => write!(f, "Missing sequence header"),
187        }
188    }
189}
190
191impl std::error::Error for MediaError {}