wtransport_proto/
error.rs

1use crate::varint::VarInt;
2use std::fmt::Debug;
3use std::fmt::Display;
4use std::fmt::Formatter;
5
6/// HTTP3 protocol errors.
7#[derive(Clone, Copy, Eq, PartialEq)]
8pub enum ErrorCode {
9    /// `H3_DATAGRAM_ERROR`.
10    Datagram,
11
12    /// `H3_NO_ERROR`.
13    NoError,
14
15    /// `H3_STREAM_CREATION_ERROR`.
16    StreamCreation,
17
18    /// `H3_CLOSED_CRITICAL_STREAM`.
19    ClosedCriticalStream,
20
21    /// `H3_FRAME_UNEXPECTED`.
22    FrameUnexpected,
23
24    /// `H3_FRAME_ERROR`.
25    Frame,
26
27    /// `H3_EXCESSIVE_LOAD`.
28    ExcessiveLoad,
29
30    /// `H3_ID_ERROR`.
31    Id,
32
33    /// `H3_SETTINGS_ERROR`.
34    Settings,
35
36    /// `H3_MISSING_SETTINGS`.
37    MissingSettings,
38
39    /// `H3_REQUEST_REJECTED`.
40    RequestRejected,
41
42    /// `H3_MESSAGE_ERROR`.
43    Message,
44
45    /// `QPACK_DECOMPRESSION_FAILED`.
46    Decompression,
47
48    /// `WEBTRANSPORT_BUFFERED_STREAM_REJECTED`.
49    BufferedStreamRejected,
50
51    /// `WEBTRANSPORT_SESSION_GONE`.
52    SessionGone,
53}
54
55impl ErrorCode {
56    /// Returns the integer representation (code) of the error.
57    pub fn to_code(self) -> VarInt {
58        match self {
59            ErrorCode::Datagram => h3_error_codes::H3_DATAGRAM_ERROR,
60            ErrorCode::NoError => h3_error_codes::H3_NO_ERROR,
61            ErrorCode::StreamCreation => h3_error_codes::H3_STREAM_CREATION_ERROR,
62            ErrorCode::ClosedCriticalStream => h3_error_codes::H3_CLOSED_CRITICAL_STREAM,
63            ErrorCode::FrameUnexpected => h3_error_codes::H3_FRAME_UNEXPECTED,
64            ErrorCode::Frame => h3_error_codes::H3_FRAME_ERROR,
65            ErrorCode::ExcessiveLoad => h3_error_codes::H3_EXCESSIVE_LOAD,
66            ErrorCode::Id => h3_error_codes::H3_ID_ERROR,
67            ErrorCode::Settings => h3_error_codes::H3_SETTINGS_ERROR,
68            ErrorCode::MissingSettings => h3_error_codes::H3_MISSING_SETTINGS,
69            ErrorCode::RequestRejected => h3_error_codes::H3_REQUEST_REJECTED,
70            ErrorCode::Message => h3_error_codes::H3_MESSAGE_ERROR,
71            ErrorCode::Decompression => qpack_error_codes::QPACK_DECOMPRESSION_FAILED,
72            ErrorCode::BufferedStreamRejected => {
73                wt_error_codes::WEBTRANSPORT_BUFFERED_STREAM_REJECTED
74            }
75            ErrorCode::SessionGone => wt_error_codes::WEBTRANSPORT_SESSION_GONE,
76        }
77    }
78}
79
80impl Debug for ErrorCode {
81    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
82        write!(f, "{} (code: {})", self, self.to_code())
83    }
84}
85
86impl Display for ErrorCode {
87    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
88        match self {
89            ErrorCode::Datagram => write!(f, "DatagramError"),
90            ErrorCode::NoError => write!(f, "NoError"),
91            ErrorCode::StreamCreation => write!(f, "StreamCreationError"),
92            ErrorCode::ClosedCriticalStream => write!(f, "ClosedCriticalStreamError"),
93            ErrorCode::FrameUnexpected => write!(f, "FrameUnexpectedError"),
94            ErrorCode::Frame => write!(f, "FrameError"),
95            ErrorCode::ExcessiveLoad => write!(f, "ExcessiveLoad"),
96            ErrorCode::Id => write!(f, "IdError"),
97            ErrorCode::Settings => write!(f, "SettingsError"),
98            ErrorCode::MissingSettings => write!(f, "MissingSettingsError"),
99            ErrorCode::RequestRejected => write!(f, "RequestRejectedError"),
100            ErrorCode::Message => write!(f, "MessageError"),
101            ErrorCode::Decompression => write!(f, "DecompressionError"),
102            ErrorCode::BufferedStreamRejected => write!(f, "BufferedStreamRejected"),
103            ErrorCode::SessionGone => write!(f, "SessionGone"),
104        }
105    }
106}
107
108impl std::error::Error for ErrorCode {}
109
110mod h3_error_codes {
111    use crate::varint::VarInt;
112
113    pub const H3_DATAGRAM_ERROR: VarInt = VarInt::from_u32(0x33);
114    pub const H3_NO_ERROR: VarInt = VarInt::from_u32(0x0100);
115    pub const H3_STREAM_CREATION_ERROR: VarInt = VarInt::from_u32(0x0103);
116    pub const H3_CLOSED_CRITICAL_STREAM: VarInt = VarInt::from_u32(0x0104);
117    pub const H3_FRAME_UNEXPECTED: VarInt = VarInt::from_u32(0x0105);
118    pub const H3_FRAME_ERROR: VarInt = VarInt::from_u32(0x0106);
119    pub const H3_EXCESSIVE_LOAD: VarInt = VarInt::from_u32(0x0107);
120    pub const H3_ID_ERROR: VarInt = VarInt::from_u32(0x0108);
121    pub const H3_SETTINGS_ERROR: VarInt = VarInt::from_u32(0x0109);
122    pub const H3_MISSING_SETTINGS: VarInt = VarInt::from_u32(0x010a);
123    pub const H3_REQUEST_REJECTED: VarInt = VarInt::from_u32(0x010b);
124    pub const H3_MESSAGE_ERROR: VarInt = VarInt::from_u32(0x010e);
125}
126
127mod qpack_error_codes {
128    use crate::varint::VarInt;
129
130    pub const QPACK_DECOMPRESSION_FAILED: VarInt = VarInt::from_u32(0x0200);
131}
132
133mod wt_error_codes {
134    use crate::varint::VarInt;
135
136    pub const WEBTRANSPORT_BUFFERED_STREAM_REJECTED: VarInt = VarInt::from_u32(0x3994_bd84);
137    pub const WEBTRANSPORT_SESSION_GONE: VarInt = VarInt::from_u32(0x170d_7b68);
138}