moq_lite/
error.rs

1use std::sync::Arc;
2
3use crate::coding;
4use web_transport_trait::{MaybeSend, MaybeSync};
5
6pub trait SendSyncError: std::error::Error + MaybeSend + MaybeSync {}
7
8impl<T> SendSyncError for T where T: std::error::Error + MaybeSend + MaybeSync {}
9
10/// A list of possible errors that can occur during the session.
11#[derive(thiserror::Error, Debug, Clone)]
12pub enum Error {
13	#[error("transport error: {0}")]
14	Transport(Arc<dyn SendSyncError>),
15
16	#[error("decode error: {0}")]
17	Decode(#[from] coding::DecodeError),
18
19	// TODO move to a ConnectError
20	#[error("unsupported versions: client={0:?} server={1:?}")]
21	Version(coding::Versions, coding::Versions),
22
23	/// A required extension was not present
24	#[error("extension required: {0}")]
25	RequiredExtension(u64),
26
27	/// An unexpected stream type was received
28	#[error("unexpected stream type")]
29	UnexpectedStream,
30
31	/// Some VarInt was too large and we were too lazy to handle it
32	#[error("varint bounds exceeded")]
33	BoundsExceeded(#[from] coding::BoundsExceeded),
34
35	/// A duplicate ID was used
36	// The broadcast/track is a duplicate
37	#[error("duplicate")]
38	Duplicate,
39
40	// Cancel is returned when there are no more readers.
41	#[error("cancelled")]
42	Cancel,
43
44	/// It took too long to open or transmit a stream.
45	#[error("timeout")]
46	Timeout,
47
48	/// The group is older than the latest group and dropped.
49	#[error("old")]
50	Old,
51
52	// The application closes the stream with a code.
53	#[error("app code={0}")]
54	App(u32),
55
56	#[error("not found")]
57	NotFound,
58
59	#[error("wrong frame size")]
60	WrongSize,
61
62	#[error("protocol violation")]
63	ProtocolViolation,
64
65	#[error("unauthorized")]
66	Unauthorized,
67
68	#[error("unexpected message")]
69	UnexpectedMessage,
70
71	#[error("unsupported")]
72	Unsupported,
73
74	#[error("too large")]
75	TooLarge,
76
77	#[error("too many parameters")]
78	TooManyParameters,
79}
80
81impl Error {
82	/// An integer code that is sent over the wire.
83	pub fn to_code(&self) -> u32 {
84		match self {
85			Self::Cancel => 0,
86			Self::RequiredExtension(_) => 1,
87			Self::Old => 2,
88			Self::Timeout => 3,
89			Self::Transport(_) => 4,
90			Self::Decode(_) => 5,
91			Self::Unauthorized => 6,
92			Self::Version(..) => 9,
93			Self::UnexpectedStream => 10,
94			Self::BoundsExceeded(_) => 11,
95			Self::Duplicate => 12,
96			Self::NotFound => 13,
97			Self::WrongSize => 14,
98			Self::ProtocolViolation => 15,
99			Self::UnexpectedMessage => 16,
100			Self::Unsupported => 17,
101			Self::TooLarge => 18,
102			Self::TooManyParameters => 19,
103			Self::App(app) => *app + 64,
104		}
105	}
106}
107
108pub type Result<T> = std::result::Result<T, Error>;