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	/// Index out of bounds.
119	#[error("no index")]
120	NoIndex,
121
122	/// Null byte found in C string.
123	#[error("nul error")]
124	NulError(#[from] std::ffi::NulError),
125}
126
127impl From<tracing::metadata::ParseLevelError> for Error {
128	fn from(err: tracing::metadata::ParseLevelError) -> Self {
129		Error::Level(Arc::new(err))
130	}
131}
132
133impl ffi::ReturnCode for Error {
134	fn code(&self) -> i32 {
135		tracing::error!("{}", self);
136		match self {
137			Error::Closed => -1,
138			Error::Moq(_) => -2,
139			Error::Url(_) => -3,
140			Error::Utf8(_) => -4,
141			Error::Connect(_) => -5,
142			Error::InvalidPointer => -6,
143			Error::InvalidId => -7,
144			Error::NotFound => -8,
145			Error::UnknownFormat(_) => -9,
146			Error::InitFailed(_) => -10,
147			Error::DecodeFailed(_) => -11,
148			Error::TimestampOverflow(_) => -13,
149			Error::Level(_) => -14,
150			Error::InvalidCode => -15,
151			Error::Panic => -16,
152			Error::Offline => -17,
153			Error::Hang(_) => -18,
154			Error::NoIndex => -19,
155			Error::NulError(_) => -20,
156			Error::SessionNotFound => -21,
157			Error::OriginNotFound => -22,
158			Error::AnnouncementNotFound => -23,
159			Error::BroadcastNotFound => -24,
160			Error::CatalogNotFound => -25,
161			Error::MediaNotFound => -26,
162			Error::TrackNotFound => -27,
163			Error::FrameNotFound => -28,
164		}
165	}
166}