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)]
16pub enum Error {
17	/// Resource was closed.
18	#[error("closed")]
19	Closed,
20
21	/// Error from the underlying MoQ protocol layer.
22	#[error("moq error: {0}")]
23	Moq(#[from] moq_lite::Error),
24
25	/// URL parsing error.
26	#[error("url error: {0}")]
27	Url(#[from] url::ParseError),
28
29	/// UTF-8 string validation error.
30	#[error("utf8 error: {0}")]
31	Utf8(#[from] std::str::Utf8Error),
32
33	/// Connection establishment error.
34	#[error("connect error: {0}")]
35	Connect(Arc<anyhow::Error>),
36
37	/// Null or invalid pointer passed from C.
38	#[error("invalid pointer")]
39	InvalidPointer,
40
41	/// Invalid resource ID.
42	#[error("invalid id")]
43	InvalidId,
44
45	/// Resource not found.
46	#[error("not found")]
47	NotFound,
48
49	/// Unknown media format specified.
50	#[error("unknown format: {0}")]
51	UnknownFormat(String),
52
53	/// Media decoder initialization failed.
54	#[error("init failed: {0}")]
55	InitFailed(Arc<anyhow::Error>),
56
57	/// Media frame decode failed.
58	#[error("decode failed: {0}")]
59	DecodeFailed(Arc<anyhow::Error>),
60
61	/// Timestamp value overflow.
62	#[error("timestamp overflow")]
63	TimestampOverflow(#[from] hang::TimestampOverflow),
64
65	/// Log level parsing error.
66	#[error("level error: {0}")]
67	Level(Arc<tracing::metadata::ParseLevelError>),
68
69	/// Invalid error code conversion.
70	#[error("invalid code")]
71	InvalidCode,
72
73	/// Panic occurred in Rust code.
74	#[error("panic")]
75	Panic,
76
77	/// Session is offline.
78	#[error("offline")]
79	Offline,
80
81	/// Error from the hang media layer.
82	#[error("hang error: {0}")]
83	Hang(#[from] hang::Error),
84
85	/// Index out of bounds.
86	#[error("no index")]
87	NoIndex,
88
89	/// Null byte found in C string.
90	#[error("nul error")]
91	NulError(#[from] std::ffi::NulError),
92}
93
94impl From<tracing::metadata::ParseLevelError> for Error {
95	fn from(err: tracing::metadata::ParseLevelError) -> Self {
96		Error::Level(Arc::new(err))
97	}
98}
99
100impl ffi::ReturnCode for Error {
101	fn code(&self) -> i32 {
102		tracing::error!("{}", self);
103		match self {
104			Error::Closed => -1,
105			Error::Moq(_) => -2,
106			Error::Url(_) => -3,
107			Error::Utf8(_) => -4,
108			Error::Connect(_) => -5,
109			Error::InvalidPointer => -6,
110			Error::InvalidId => -7,
111			Error::NotFound => -8,
112			Error::UnknownFormat(_) => -9,
113			Error::InitFailed(_) => -10,
114			Error::DecodeFailed(_) => -11,
115			Error::TimestampOverflow(_) => -13,
116			Error::Level(_) => -14,
117			Error::InvalidCode => -15,
118			Error::Panic => -16,
119			Error::Offline => -17,
120			Error::Hang(_) => -18,
121			Error::NoIndex => -19,
122			Error::NulError(_) => -20,
123		}
124	}
125}