Skip to main content

moq_lite/
error.rs

1use crate::coding;
2
3/// A list of possible errors that can occur during the session.
4#[derive(thiserror::Error, Debug, Clone)]
5#[non_exhaustive]
6pub enum Error {
7	#[error("transport: {0}")]
8	Transport(String),
9
10	#[error(transparent)]
11	Decode(#[from] coding::DecodeError),
12
13	// TODO move to a ConnectError
14	#[error("unsupported versions")]
15	Version,
16
17	/// A required extension was not present
18	#[error("extension required")]
19	RequiredExtension,
20
21	/// An unexpected stream type was received
22	#[error("unexpected stream type")]
23	UnexpectedStream,
24
25	#[error(transparent)]
26	BoundsExceeded(#[from] coding::BoundsExceeded),
27
28	/// A duplicate ID was used
29	// The broadcast/track is a duplicate
30	#[error("duplicate")]
31	Duplicate,
32
33	// Cancel is returned when there are no more readers.
34	#[error("cancelled")]
35	Cancel,
36
37	/// It took too long to open or transmit a stream.
38	#[error("timeout")]
39	Timeout,
40
41	/// The group is older than the latest group and dropped.
42	#[error("old")]
43	Old,
44
45	// The application closes the stream with a code.
46	#[error("app code={0}")]
47	App(u16),
48
49	#[error("not found")]
50	NotFound,
51
52	#[error("wrong frame size")]
53	WrongSize,
54
55	#[error("protocol violation")]
56	ProtocolViolation,
57
58	#[error("unauthorized")]
59	Unauthorized,
60
61	#[error("unexpected message")]
62	UnexpectedMessage,
63
64	#[error("unsupported")]
65	Unsupported,
66
67	#[error(transparent)]
68	Encode(#[from] coding::EncodeError),
69
70	#[error("too many parameters")]
71	TooManyParameters,
72
73	#[error("invalid role")]
74	InvalidRole,
75
76	#[error("unknown ALPN: {0}")]
77	UnknownAlpn(String),
78
79	#[error("dropped")]
80	Dropped,
81
82	#[error("closed")]
83	Closed,
84
85	/// The cached frame has been evicted due to the group size limit.
86	#[error("cache full")]
87	CacheFull,
88
89	/// A remote error received via a stream/session reset code.
90	#[error("remote error: code={0}")]
91	Remote(u32),
92}
93
94impl Error {
95	/// An integer code that is sent over the wire.
96	pub fn to_code(&self) -> u32 {
97		match self {
98			Self::Cancel => 0,
99			Self::RequiredExtension => 1,
100			Self::Old => 2,
101			Self::Timeout => 3,
102			Self::Transport(_) => 4,
103			Self::Decode(_) => 5,
104			Self::Unauthorized => 6,
105			Self::Version => 9,
106			Self::UnexpectedStream => 10,
107			Self::BoundsExceeded(_) => 11,
108			Self::Duplicate => 12,
109			Self::NotFound => 13,
110			Self::WrongSize => 14,
111			Self::ProtocolViolation => 15,
112			Self::UnexpectedMessage => 16,
113			Self::Unsupported => 17,
114			Self::Encode(_) => 18,
115			Self::TooManyParameters => 19,
116			Self::InvalidRole => 20,
117			Self::UnknownAlpn(_) => 21,
118			Self::Dropped => 24,
119			Self::Closed => 25,
120			Self::CacheFull => 26,
121			Self::App(app) => *app as u32 + 64,
122			Self::Remote(code) => *code,
123		}
124	}
125
126	/// Convert a transport error into an [Error], decoding stream reset codes.
127	pub fn from_transport(err: impl web_transport_trait::Error) -> Self {
128		if let Some(code) = err.stream_error() {
129			return Self::Remote(code);
130		}
131
132		Self::Transport(err.to_string())
133	}
134}
135
136impl web_transport_trait::Error for Error {
137	fn session_error(&self) -> Option<(u32, String)> {
138		None
139	}
140}
141
142pub type Result<T> = std::result::Result<T, Error>;