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#[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 #[error("unsupported versions: client={0:?} server={1:?}")]
21 Version(coding::Versions, coding::Versions),
22
23 #[error("extension required: {0}")]
25 RequiredExtension(u64),
26
27 #[error("unexpected stream type")]
29 UnexpectedStream,
30
31 #[error("varint bounds exceeded")]
33 BoundsExceeded(#[from] coding::BoundsExceeded),
34
35 #[error("duplicate")]
38 Duplicate,
39
40 #[error("cancelled")]
42 Cancel,
43
44 #[error("timeout")]
46 Timeout,
47
48 #[error("old")]
50 Old,
51
52 #[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
75impl Error {
76 pub fn to_code(&self) -> u32 {
78 match self {
79 Self::Cancel => 0,
80 Self::RequiredExtension(_) => 1,
81 Self::Old => 2,
82 Self::Timeout => 3,
83 Self::Transport(_) => 4,
84 Self::Decode(_) => 5,
85 Self::Unauthorized => 6,
86 Self::Version(..) => 9,
87 Self::UnexpectedStream => 10,
88 Self::BoundsExceeded(_) => 11,
89 Self::Duplicate => 12,
90 Self::NotFound => 13,
91 Self::WrongSize => 14,
92 Self::ProtocolViolation => 15,
93 Self::UnexpectedMessage => 16,
94 Self::Unsupported => 17,
95 Self::App(app) => *app + 64,
96 }
97 }
98}
99
100pub type Result<T> = std::result::Result<T, Error>;