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	/// Unknown media format specified.
51	#[error("unknown format: {0}")]
52	UnknownFormat(String),
53
54	/// Media decoder initialization failed.
55	#[error("init failed: {0}")]
56	InitFailed(Arc<anyhow::Error>),
57
58	/// Media frame decode failed.
59	#[error("decode failed: {0}")]
60	DecodeFailed(Arc<anyhow::Error>),
61
62	/// Timestamp value overflow.
63	#[error("timestamp overflow")]
64	TimestampOverflow(#[from] moq_lite::TimeOverflow),
65
66	/// Log level parsing error.
67	#[error("level error: {0}")]
68	Level(Arc<tracing::metadata::ParseLevelError>),
69
70	/// Invalid error code conversion.
71	#[error("invalid code")]
72	InvalidCode,
73
74	/// Panic occurred in Rust code.
75	#[error("panic")]
76	Panic,
77
78	/// Session is offline.
79	#[error("offline")]
80	Offline,
81
82	/// Error from the hang media layer.
83	#[error("hang error: {0}")]
84	Hang(#[from] hang::Error),
85
86	/// Index out of bounds.
87	#[error("no index")]
88	NoIndex,
89
90	/// Null byte found in C string.
91	#[error("nul error")]
92	NulError(#[from] std::ffi::NulError),
93}
94
95impl From<tracing::metadata::ParseLevelError> for Error {
96	fn from(err: tracing::metadata::ParseLevelError) -> Self {
97		Error::Level(Arc::new(err))
98	}
99}
100
101impl ffi::ReturnCode for Error {
102	fn code(&self) -> i32 {
103		tracing::error!("{}", self);
104		match self {
105			Error::Closed => -1,
106			Error::Moq(_) => -2,
107			Error::Url(_) => -3,
108			Error::Utf8(_) => -4,
109			Error::Connect(_) => -5,
110			Error::InvalidPointer => -6,
111			Error::InvalidId => -7,
112			Error::NotFound => -8,
113			Error::UnknownFormat(_) => -9,
114			Error::InitFailed(_) => -10,
115			Error::DecodeFailed(_) => -11,
116			Error::TimestampOverflow(_) => -13,
117			Error::Level(_) => -14,
118			Error::InvalidCode => -15,
119			Error::Panic => -16,
120			Error::Offline => -17,
121			Error::Hang(_) => -18,
122			Error::NoIndex => -19,
123			Error::NulError(_) => -20,
124		}
125	}
126}