Skip to main content

oxideav_rtmp/
error.rs

1//! Single error enum covering every way RTMP goes wrong.
2
3use std::fmt;
4use std::io;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug)]
9pub enum Error {
10    /// Underlying TCP / read / write I/O failure.
11    Io(io::Error),
12    /// Peer closed the connection mid-protocol.
13    UnexpectedEof,
14    /// Handshake byte 0 was not the expected version 3.
15    UnsupportedHandshakeVersion(u8),
16    /// Saw bytes that don't match any valid AMF0 marker.
17    InvalidAmf0(String),
18    /// Saw a chunk message header shape or reserved value we don't
19    /// know how to interpret.
20    InvalidChunk(String),
21    /// Command message arrived without the expected name / transaction
22    /// id / args (e.g. `connect` missing its command object).
23    InvalidCommand(String),
24    /// A sub-protocol field (window ack size, chunk size, peer
25    /// bandwidth) carries a value outside its legal range.
26    ProtocolViolation(String),
27    /// Consumer rejected a publish attempt via `PublishRequest::reject`.
28    /// The string is the reason passed to `onStatus`.
29    Rejected(String),
30    /// We waited for a specific message and the deadline elapsed.
31    Timeout,
32    /// Generic bucket for situations that don't deserve a dedicated
33    /// variant (e.g. malformed AVC sequence header payload).
34    Other(String),
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Error::Io(e) => write!(f, "rtmp i/o: {e}"),
41            Error::UnexpectedEof => write!(f, "rtmp: peer closed connection"),
42            Error::UnsupportedHandshakeVersion(v) => {
43                write!(f, "rtmp handshake: unsupported version byte {v:#x}")
44            }
45            Error::InvalidAmf0(m) => write!(f, "rtmp amf0: {m}"),
46            Error::InvalidChunk(m) => write!(f, "rtmp chunk: {m}"),
47            Error::InvalidCommand(m) => write!(f, "rtmp command: {m}"),
48            Error::ProtocolViolation(m) => write!(f, "rtmp protocol: {m}"),
49            Error::Rejected(reason) => write!(f, "rtmp: publish rejected: {reason}"),
50            Error::Timeout => write!(f, "rtmp: timeout"),
51            Error::Other(m) => write!(f, "rtmp: {m}"),
52        }
53    }
54}
55
56impl std::error::Error for Error {
57    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
58        match self {
59            Error::Io(e) => Some(e),
60            _ => None,
61        }
62    }
63}
64
65impl From<io::Error> for Error {
66    fn from(e: io::Error) -> Self {
67        Error::Io(e)
68    }
69}