moq_lite/
error.rs

1use crate::{coding, message};
2
3/// A list of possible errors that can occur during the session.
4#[derive(thiserror::Error, Debug, Clone)]
5pub enum Error {
6	#[error("webtransport error: {0}")]
7	WebTransport(#[from] web_transport::Error),
8
9	#[error("decode error: {0}")]
10	Decode(#[from] coding::DecodeError),
11
12	// TODO move to a ConnectError
13	#[error("unsupported versions: client={0:?} server={1:?}")]
14	Version(message::Versions, message::Versions),
15
16	/// A required extension was not present
17	#[error("extension required: {0}")]
18	RequiredExtension(u64),
19
20	/// An unexpected stream was received
21	#[error("unexpected stream: {0:?}")]
22	UnexpectedStream(message::ControlType),
23
24	/// Some VarInt was too large and we were too lazy to handle it
25	#[error("varint bounds exceeded")]
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(u32),
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
62impl Error {
63	/// An integer code that is sent over the wire.
64	pub fn to_code(&self) -> u32 {
65		match self {
66			Self::Cancel => 0,
67			Self::RequiredExtension(_) => 1,
68			Self::Old => 2,
69			Self::Timeout => 3,
70			Self::WebTransport(_) => 4,
71			Self::Decode(_) => 5,
72			Self::Unauthorized => 6,
73			Self::Version(..) => 9,
74			Self::UnexpectedStream(_) => 10,
75			Self::BoundsExceeded(_) => 11,
76			Self::Duplicate => 12,
77			Self::NotFound => 13,
78			Self::WrongSize => 14,
79			Self::ProtocolViolation => 15,
80			Self::App(app) => *app + 64,
81		}
82	}
83}
84
85pub type Result<T> = std::result::Result<T, Error>;