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
59impl Error {
60	/// An integer code that is sent over the wire.
61	pub fn to_code(&self) -> u32 {
62		match self {
63			Self::Cancel => 0,
64			Self::RequiredExtension(_) => 1,
65			Self::Old => 2,
66			Self::Timeout => 3,
67			Self::WebTransport(_) => 4,
68			Self::Decode(_) => 5,
69			Self::Version(..) => 9,
70			Self::UnexpectedStream(_) => 10,
71			Self::BoundsExceeded(_) => 11,
72			Self::Duplicate => 12,
73			Self::NotFound => 13,
74			Self::WrongSize => 14,
75			Self::ProtocolViolation => 15,
76			Self::App(app) => *app + 64,
77		}
78	}
79}
80
81pub type Result<T> = std::result::Result<T, Error>;