Skip to main content

moq/
error.rs

1use std::sync::Arc;
2
3use crate::ffi;
4
5/// Status code returned by FFI functions.
6///
7/// Negative values indicate errors, zero indicates success,
8/// and positive values are valid resource handles.
9pub type Status = i32;
10
11/// Error types that can occur in the FFI layer.
12///
13/// Each error variant maps to a specific negative error code
14/// returned to C callers.
15#[derive(Debug, thiserror::Error, Clone)]
16#[non_exhaustive]
17pub enum Error {
18	/// Resource was closed.
19	#[error("closed")]
20	Closed,
21
22	/// Error from the underlying MoQ protocol layer.
23	#[error("moq error: {0}")]
24	Moq(#[from] moq_net::Error),
25
26	/// URL parsing error.
27	#[error("url error: {0}")]
28	Url(#[from] url::ParseError),
29
30	/// UTF-8 string validation error.
31	#[error("utf8 error: {0}")]
32	Utf8(#[from] std::str::Utf8Error),
33
34	/// Connection establishment error.
35	#[error("connect error: {0}")]
36	Connect(Arc<anyhow::Error>),
37
38	/// Null or invalid pointer passed from C.
39	#[error("invalid pointer")]
40	InvalidPointer,
41
42	/// Invalid resource ID.
43	#[error("invalid id")]
44	InvalidId,
45
46	/// Resource not found.
47	#[error("not found")]
48	NotFound,
49
50	/// Session task not found.
51	#[error("session not found")]
52	SessionNotFound,
53
54	/// Origin producer not found.
55	#[error("origin not found")]
56	OriginNotFound,
57
58	/// Announcement not found.
59	#[error("announcement not found")]
60	AnnouncementNotFound,
61
62	/// Broadcast not found.
63	#[error("broadcast not found")]
64	BroadcastNotFound,
65
66	/// Catalog not found.
67	#[error("catalog not found")]
68	CatalogNotFound,
69
70	/// Media decoder not found.
71	#[error("media not found")]
72	MediaNotFound,
73
74	/// Track task not found.
75	#[error("track not found")]
76	TrackNotFound,
77
78	/// Frame not found.
79	#[error("frame not found")]
80	FrameNotFound,
81
82	/// Unknown media format specified.
83	#[error("unknown format: {0}")]
84	UnknownFormat(String),
85
86	/// Media decoder initialization failed.
87	#[error("init failed: {0}")]
88	InitFailed(Arc<anyhow::Error>),
89
90	/// Media frame decode failed.
91	#[error("decode failed: {0}")]
92	DecodeFailed(Arc<anyhow::Error>),
93
94	/// Timestamp value overflow.
95	#[error("timestamp overflow")]
96	TimestampOverflow(#[from] moq_net::TimeOverflow),
97
98	/// Log level parsing error.
99	#[error("level error: {0}")]
100	Level(Arc<tracing::metadata::ParseLevelError>),
101
102	/// Invalid error code conversion.
103	#[error("invalid code")]
104	InvalidCode,
105
106	/// Panic occurred in Rust code.
107	#[error("panic")]
108	Panic,
109
110	/// Session is offline.
111	#[error("offline")]
112	Offline,
113
114	/// Error from the hang media layer.
115	#[error("hang error: {0}")]
116	Hang(#[from] hang::Error),
117
118	/// Error from the moq-mux consumer layer.
119	#[error("mux error: {0}")]
120	Mux(Arc<moq_mux::Error>),
121
122	/// Index out of bounds.
123	#[error("no index")]
124	NoIndex,
125
126	/// Null byte found in C string.
127	#[error("nul error")]
128	NulError(#[from] std::ffi::NulError),
129
130	/// Error from the moq-audio codec layer.
131	#[error("audio error: {0}")]
132	Audio(Arc<moq_audio::AudioError>),
133}
134
135impl From<moq_audio::AudioError> for Error {
136	fn from(err: moq_audio::AudioError) -> Self {
137		Error::Audio(Arc::new(err))
138	}
139}
140
141impl From<tracing::metadata::ParseLevelError> for Error {
142	fn from(err: tracing::metadata::ParseLevelError) -> Self {
143		Error::Level(Arc::new(err))
144	}
145}
146
147impl From<moq_mux::Error> for Error {
148	fn from(err: moq_mux::Error) -> Self {
149		match err {
150			moq_mux::Error::Moq(e) => Error::Moq(e),
151			moq_mux::Error::Hang(e) => Error::Hang(e),
152			e => Error::Mux(Arc::new(e)),
153		}
154	}
155}
156
157impl ffi::ReturnCode for Error {
158	fn code(&self) -> i32 {
159		tracing::error!("{}", self);
160		match self {
161			Error::Closed => -1,
162			Error::Moq(_) => -2,
163			Error::Url(_) => -3,
164			Error::Utf8(_) => -4,
165			Error::Connect(_) => -5,
166			Error::InvalidPointer => -6,
167			Error::InvalidId => -7,
168			Error::NotFound => -8,
169			Error::UnknownFormat(_) => -9,
170			Error::InitFailed(_) => -10,
171			Error::DecodeFailed(_) => -11,
172			Error::TimestampOverflow(_) => -13,
173			Error::Level(_) => -14,
174			Error::InvalidCode => -15,
175			Error::Panic => -16,
176			Error::Offline => -17,
177			Error::Hang(_) => -18,
178			Error::NoIndex => -19,
179			Error::NulError(_) => -20,
180			Error::SessionNotFound => -21,
181			Error::OriginNotFound => -22,
182			Error::AnnouncementNotFound => -23,
183			Error::BroadcastNotFound => -24,
184			Error::CatalogNotFound => -25,
185			Error::MediaNotFound => -26,
186			Error::TrackNotFound => -27,
187			Error::FrameNotFound => -28,
188			Error::Mux(_) => -29,
189			Error::Audio(_) => -30,
190		}
191	}
192}