moq_lite/
error.rs

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