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	/// Error from the underlying MoQ protocol layer.
19	#[error("moq error: {0}")]
20	Moq(#[from] moq_net::Error),
21
22	/// Error from the native helper layer (moq-native).
23	#[error("native error: {0}")]
24	Native(#[from] moq_native::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	/// Group producer not found.
79	#[error("group not found")]
80	GroupNotFound,
81
82	/// Frame not found.
83	#[error("frame not found")]
84	FrameNotFound,
85
86	/// Unknown media format specified.
87	#[error("unknown format: {0}")]
88	UnknownFormat(String),
89
90	/// Media decoder initialization failed.
91	#[error("init failed: {0}")]
92	InitFailed(Arc<anyhow::Error>),
93
94	/// Media frame decode failed.
95	#[error("decode failed: {0}")]
96	DecodeFailed(Arc<anyhow::Error>),
97
98	/// Timestamp value overflow.
99	#[error("timestamp overflow")]
100	TimestampOverflow(#[from] moq_net::TimeOverflow),
101
102	/// Log level parsing error.
103	#[error("level error: {0}")]
104	Level(Arc<tracing::metadata::ParseLevelError>),
105
106	/// Invalid error code conversion.
107	#[error("invalid code")]
108	InvalidCode,
109
110	/// Panic occurred in Rust code.
111	#[error("panic")]
112	Panic,
113
114	/// Session is offline.
115	#[error("offline")]
116	Offline,
117
118	/// Error from the hang media layer.
119	#[error("hang error: {0}")]
120	Hang(#[from] hang::Error),
121
122	/// Error from the moq-mux consumer layer.
123	#[error("mux error: {0}")]
124	Mux(Arc<moq_mux::Error>),
125
126	/// Index out of bounds.
127	#[error("no index")]
128	NoIndex,
129
130	/// Null byte found in C string.
131	#[error("nul error")]
132	NulError(#[from] std::ffi::NulError),
133
134	/// Error from the moq-audio codec layer.
135	#[error("audio error: {0}")]
136	Audio(Arc<moq_audio::AudioError>),
137}
138
139impl From<moq_audio::AudioError> for Error {
140	fn from(err: moq_audio::AudioError) -> Self {
141		Error::Audio(Arc::new(err))
142	}
143}
144
145impl From<tracing::metadata::ParseLevelError> for Error {
146	fn from(err: tracing::metadata::ParseLevelError) -> Self {
147		Error::Level(Arc::new(err))
148	}
149}
150
151impl From<moq_mux::Error> for Error {
152	fn from(err: moq_mux::Error) -> Self {
153		match err {
154			moq_mux::Error::Moq(e) => Error::Moq(e),
155			moq_mux::Error::Hang(e) => Error::Hang(e),
156			e => Error::Mux(Arc::new(e)),
157		}
158	}
159}
160
161impl ffi::ReturnCode for Error {
162	fn error(&self) -> Option<&Error> {
163		Some(self)
164	}
165
166	fn code(&self) -> i32 {
167		match self {
168			Error::Moq(_) => -2,
169			Error::Url(_) => -3,
170			Error::Utf8(_) => -4,
171			Error::Connect(_) => -5,
172			Error::InvalidPointer => -6,
173			Error::InvalidId => -7,
174			Error::NotFound => -8,
175			Error::UnknownFormat(_) => -9,
176			Error::InitFailed(_) => -10,
177			Error::DecodeFailed(_) => -11,
178			Error::TimestampOverflow(_) => -13,
179			Error::Level(_) => -14,
180			Error::InvalidCode => -15,
181			Error::Panic => -16,
182			Error::Offline => -17,
183			Error::Hang(_) => -18,
184			Error::NoIndex => -19,
185			Error::NulError(_) => -20,
186			Error::SessionNotFound => -21,
187			Error::OriginNotFound => -22,
188			Error::AnnouncementNotFound => -23,
189			Error::BroadcastNotFound => -24,
190			Error::CatalogNotFound => -25,
191			Error::MediaNotFound => -26,
192			Error::TrackNotFound => -27,
193			Error::FrameNotFound => -28,
194			Error::Mux(_) => -29,
195			Error::Audio(_) => -30,
196			Error::GroupNotFound => -31,
197			Error::Native(_) => -32,
198		}
199	}
200}