1use std::sync::Arc;
2
3use crate::{coding, message};
4
5#[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 #[error("unsupported versions: client={0:?} server={1:?}")]
16 Version(message::Versions, message::Versions),
17
18 #[error("extension required: {0}")]
20 RequiredExtension(u64),
21
22 #[error("unexpected stream: {0:?}")]
24 UnexpectedStream(message::ControlType),
25
26 #[error("varint bounds exceeded")]
28 BoundsExceeded(#[from] coding::BoundsExceeded),
29
30 #[error("duplicate")]
33 Duplicate,
34
35 #[error("cancelled")]
37 Cancel,
38
39 #[error("timeout")]
41 Timeout,
42
43 #[error("old")]
45 Old,
46
47 #[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 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>;