moq_lite/
error.rs

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