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_lite::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_lite::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
131impl From<tracing::metadata::ParseLevelError> for Error {
132	fn from(err: tracing::metadata::ParseLevelError) -> Self {
133		Error::Level(Arc::new(err))
134	}
135}
136
137impl From<moq_mux::Error> for Error {
138	fn from(err: moq_mux::Error) -> Self {
139		match err {
140			moq_mux::Error::Moq(e) => Error::Moq(e),
141			moq_mux::Error::Hang(e) => Error::Hang(e),
142			e => Error::Mux(Arc::new(e)),
143		}
144	}
145}
146
147impl ffi::ReturnCode for Error {
148	fn code(&self) -> i32 {
149		tracing::error!("{}", self);
150		match self {
151			Error::Closed => -1,
152			Error::Moq(_) => -2,
153			Error::Url(_) => -3,
154			Error::Utf8(_) => -4,
155			Error::Connect(_) => -5,
156			Error::InvalidPointer => -6,
157			Error::InvalidId => -7,
158			Error::NotFound => -8,
159			Error::UnknownFormat(_) => -9,
160			Error::InitFailed(_) => -10,
161			Error::DecodeFailed(_) => -11,
162			Error::TimestampOverflow(_) => -13,
163			Error::Level(_) => -14,
164			Error::InvalidCode => -15,
165			Error::Panic => -16,
166			Error::Offline => -17,
167			Error::Hang(_) => -18,
168			Error::NoIndex => -19,
169			Error::NulError(_) => -20,
170			Error::SessionNotFound => -21,
171			Error::OriginNotFound => -22,
172			Error::AnnouncementNotFound => -23,
173			Error::BroadcastNotFound => -24,
174			Error::CatalogNotFound => -25,
175			Error::MediaNotFound => -26,
176			Error::TrackNotFound => -27,
177			Error::FrameNotFound => -28,
178			Error::Mux(_) => -29,
179		}
180	}
181}