moq_transfork/
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	// The application closes the stream with a code.
38	#[error("app code={0}")]
39	App(u32),
40
41	#[error("not found")]
42	NotFound,
43
44	#[error("wrong frame size")]
45	WrongSize,
46
47	#[error("protocol violation")]
48	ProtocolViolation,
49}
50
51impl Error {
52	/// An integer code that is sent over the wire.
53	pub fn to_code(&self) -> u32 {
54		match self {
55			Self::Cancel => 0,
56			Self::RequiredExtension(_) => 1,
57			Self::WebTransport(_) => 4,
58			Self::Decode(_) => 5,
59			Self::Version(..) => 9,
60			Self::UnexpectedStream(_) => 10,
61			Self::BoundsExceeded(_) => 11,
62			Self::Duplicate => 12,
63			Self::NotFound => 13,
64			Self::WrongSize => 14,
65			Self::ProtocolViolation => 15,
66			Self::App(app) => *app + 64,
67		}
68	}
69}
70
71pub type Result<T> = std::result::Result<T, Error>;