Skip to main content

moq_net/
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 frame declared a payload size larger than the receiver accepts.
90	#[error("frame too large")]
91	FrameTooLarge,
92
93	/// A remote error received via a stream/session reset code.
94	#[error("remote error: code={0}")]
95	Remote(u32),
96}
97
98impl Error {
99	/// An integer code that is sent over the wire.
100	pub fn to_code(&self) -> u32 {
101		match self {
102			Self::Cancel => 0,
103			Self::RequiredExtension => 1,
104			Self::Old => 2,
105			Self::Timeout => 3,
106			Self::Transport(_) => 4,
107			Self::Decode(_) => 5,
108			Self::Unauthorized => 6,
109			Self::Version => 9,
110			Self::UnexpectedStream => 10,
111			Self::BoundsExceeded(_) => 11,
112			Self::Duplicate => 12,
113			Self::NotFound => 13,
114			Self::WrongSize => 14,
115			Self::ProtocolViolation => 15,
116			Self::UnexpectedMessage => 16,
117			Self::Unsupported => 17,
118			Self::Encode(_) => 18,
119			Self::TooManyParameters => 19,
120			Self::InvalidRole => 20,
121			Self::UnknownAlpn(_) => 21,
122			Self::Dropped => 24,
123			Self::Closed => 25,
124			Self::CacheFull => 26,
125			Self::FrameTooLarge => 27,
126			Self::App(app) => *app as u32 + 64,
127			Self::Remote(code) => *code,
128		}
129	}
130
131	/// Convert a transport error into an [Error], decoding stream reset codes.
132	pub fn from_transport(err: impl web_transport_trait::Error) -> Self {
133		if let Some(code) = err.stream_error() {
134			return Self::Remote(code);
135		}
136
137		Self::Transport(err.to_string())
138	}
139}
140
141impl web_transport_trait::Error for Error {
142	fn session_error(&self) -> Option<(u32, String)> {
143		None
144	}
145}
146
147pub type Result<T> = std::result::Result<T, Error>;