1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u32)]
4pub enum ErrorCode {
5 NoError = 0x0,
6 ProtocolError = 0x1,
7 InternalError = 0x2,
8 FlowControlError = 0x3,
9 SettingsTimeout = 0x4,
10 StreamClosed = 0x5,
11 FrameSizeError = 0x6,
12 RefusedStream = 0x7,
13 Cancel = 0x8,
14 CompressionError = 0x9,
15 ConnectError = 0xa,
16 EnhanceYourCalm = 0xb,
17 InadequateSecurity = 0xc,
18 Http11Required = 0xd,
19}
20
21impl ErrorCode {
22 pub fn from_u32(v: u32) -> Self {
23 match v {
24 0x0 => Self::NoError,
25 0x1 => Self::ProtocolError,
26 0x2 => Self::InternalError,
27 0x3 => Self::FlowControlError,
28 0x4 => Self::SettingsTimeout,
29 0x5 => Self::StreamClosed,
30 0x6 => Self::FrameSizeError,
31 0x7 => Self::RefusedStream,
32 0x8 => Self::Cancel,
33 0x9 => Self::CompressionError,
34 0xa => Self::ConnectError,
35 0xb => Self::EnhanceYourCalm,
36 0xc => Self::InadequateSecurity,
37 0xd => Self::Http11Required,
38 _ => Self::InternalError,
39 }
40 }
41}
42
43#[derive(Debug)]
45pub enum H2Error {
46 FrameError,
48 ProtocolError(String),
50 CompressionError,
52 FlowControlError,
54 FrameSizeError,
56 ConnectionError(ErrorCode),
58 StreamError(u32, ErrorCode),
60 Internal(String),
62}
63
64impl std::fmt::Display for H2Error {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 match self {
67 Self::FrameError => write!(f, "frame error"),
68 Self::ProtocolError(s) => write!(f, "protocol error: {s}"),
69 Self::CompressionError => write!(f, "HPACK compression error"),
70 Self::FlowControlError => write!(f, "flow control error"),
71 Self::FrameSizeError => write!(f, "frame size error"),
72 Self::ConnectionError(code) => write!(f, "connection error: {code:?}"),
73 Self::StreamError(id, code) => write!(f, "stream {id} error: {code:?}"),
74 Self::Internal(s) => write!(f, "internal: {s}"),
75 }
76 }
77}
78
79impl std::error::Error for H2Error {}