1use std::borrow::Cow;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
8pub enum H3ErrorCode {
9 #[error("No error. Used when closing without an error to signal.")]
11 NoError = 0x0100,
12
13 #[error("Peer violated protocol requirements.")]
15 GeneralProtocolError = 0x0101,
16
17 #[error("An internal error in the HTTP stack.")]
19 InternalError = 0x0102,
20
21 #[error("Peer created a stream that will not be accepted.")]
23 StreamCreationError = 0x0103,
24
25 #[error("A required stream was closed or reset.")]
27 ClosedCriticalStream = 0x0104,
28
29 #[error("A frame was not permitted in the current state or stream.")]
31 FrameUnexpected = 0x0105,
32
33 #[error("A frame fails layout requirements or has an invalid size.")]
35 FrameError = 0x0106,
36
37 #[error("Peer is generating excessive load.")]
39 ExcessiveLoad = 0x0107,
40
41 #[error("A stream ID or push ID was used incorrectly.")]
43 IdError = 0x0108,
44
45 #[error("Error in the payload of a SETTINGS frame.")]
47 SettingsError = 0x0109,
48
49 #[error("No SETTINGS frame at the beginning of the control stream.")]
51 MissingSettings = 0x010a,
52
53 #[error("Server rejected a request without application processing.")]
55 RequestRejected = 0x010b,
56
57 #[error("Request or response (including pushed) is cancelled.")]
59 RequestCancelled = 0x010c,
60
61 #[error("Client stream terminated without a fully formed request.")]
63 RequestIncomplete = 0x010d,
64
65 #[error("HTTP message was malformed.")]
67 MessageError = 0x010e,
68
69 #[error("TCP connection for CONNECT was reset or abnormally closed.")]
71 ConnectError = 0x010f,
72
73 #[error("Requested operation cannot be served over HTTP/3.")]
75 VersionFallback = 0x0110,
76
77 #[error("WebTransport data stream rejected due to lack of associated session.")]
80 WebTransportBufferedStreamRejected = 0x3994_bd84,
81
82 #[error("WebTransport session gone.")]
84 WebTransportSessionGone = 0x170d_7b68,
85
86 #[error("WebTransport flow control error.")]
88 WebTransportFlowControlError = 0x045d_4487,
89
90 #[error("WebTransport ALPN error.")]
92 WebTransportAlpnError = 0x0817_b3dd,
93
94 #[error("WebTransport requirements not met.")]
96 WebTransportRequirementsNotMet = 0x212c_0d48,
97}
98
99impl H3ErrorCode {
100 pub fn reason(&self) -> Cow<'static, str> {
102 Cow::Owned(format!("{self}"))
104 }
105
106 pub fn is_connection_error(&self) -> bool {
113 matches!(
114 self,
115 Self::GeneralProtocolError
116 | Self::InternalError
117 | Self::ClosedCriticalStream
118 | Self::FrameUnexpected
119 | Self::FrameError
120 | Self::ExcessiveLoad
121 | Self::IdError
122 | Self::SettingsError
123 | Self::MissingSettings
124 )
125 }
126}
127
128impl From<u64> for H3ErrorCode {
129 fn from(value: u64) -> Self {
132 match value {
133 0x0101 => Self::GeneralProtocolError,
134 0x0102 => Self::InternalError,
135 0x0103 => Self::StreamCreationError,
136 0x0104 => Self::ClosedCriticalStream,
137 0x0105 => Self::FrameUnexpected,
138 0x0106 => Self::FrameError,
139 0x0107 => Self::ExcessiveLoad,
140 0x0108 => Self::IdError,
141 0x0109 => Self::SettingsError,
142 0x010a => Self::MissingSettings,
143 0x010b => Self::RequestRejected,
144 0x010c => Self::RequestCancelled,
145 0x010d => Self::RequestIncomplete,
146 0x010e => Self::MessageError,
147 0x010f => Self::ConnectError,
148 0x0110 => Self::VersionFallback,
149 0x3994_bd84 => Self::WebTransportBufferedStreamRejected,
150 0x170d_7b68 => Self::WebTransportSessionGone,
151 0x045d_4487 => Self::WebTransportFlowControlError,
152 0x0817_b3dd => Self::WebTransportAlpnError,
153 0x212c_0d48 => Self::WebTransportRequirementsNotMet,
154 _ => Self::NoError,
155 }
156 }
157}
158
159impl From<H3ErrorCode> for u64 {
160 fn from(code: H3ErrorCode) -> u64 {
164 match code {
165 H3ErrorCode::NoError => {
166 let n = u64::from(fastrand::u16(..));
167 0x1f * n + 0x21
168 }
169 other => other as u64,
170 }
171 }
172}
173
174#[cfg(test)]
175mod tests {
176 use super::*;
177
178 #[test]
179 fn known_codes_roundtrip() {
180 for code in [
181 H3ErrorCode::GeneralProtocolError,
182 H3ErrorCode::InternalError,
183 H3ErrorCode::StreamCreationError,
184 H3ErrorCode::ClosedCriticalStream,
185 H3ErrorCode::FrameUnexpected,
186 H3ErrorCode::FrameError,
187 H3ErrorCode::ExcessiveLoad,
188 H3ErrorCode::IdError,
189 H3ErrorCode::SettingsError,
190 H3ErrorCode::MissingSettings,
191 H3ErrorCode::RequestRejected,
192 H3ErrorCode::RequestCancelled,
193 H3ErrorCode::RequestIncomplete,
194 H3ErrorCode::MessageError,
195 H3ErrorCode::ConnectError,
196 H3ErrorCode::VersionFallback,
197 H3ErrorCode::WebTransportBufferedStreamRejected,
198 H3ErrorCode::WebTransportSessionGone,
199 H3ErrorCode::WebTransportFlowControlError,
200 H3ErrorCode::WebTransportAlpnError,
201 H3ErrorCode::WebTransportRequirementsNotMet,
202 ] {
203 let wire: u64 = code.into();
204 let decoded = H3ErrorCode::from(wire);
205 assert_eq!(decoded, code, "roundtrip failed for {code:?}");
206 }
207 }
208
209 #[test]
210 fn no_error_encodes_as_grease() {
211 for _ in 0..100 {
212 let wire: u64 = H3ErrorCode::NoError.into();
213 assert_ne!(wire, 0x0100, "should emit GREASE, not literal NoError");
214 assert_eq!(
215 (wire - 0x21) % 0x1f,
216 0,
217 "{wire:#x} is not a valid GREASE value"
218 );
219 }
220 }
221
222 #[test]
223 fn grease_decodes_as_no_error() {
224 for n in [0u64, 1, 100, 0xFFFF] {
225 let grease = 0x1f * n + 0x21;
226 assert_eq!(H3ErrorCode::from(grease), H3ErrorCode::NoError);
227 }
228 }
229
230 #[test]
231 fn unknown_non_grease_decodes_as_no_error() {
232 assert_eq!(H3ErrorCode::from(0xDEAD), H3ErrorCode::NoError);
233 assert_eq!(H3ErrorCode::from(0), H3ErrorCode::NoError);
234 assert_eq!(H3ErrorCode::from(u64::MAX), H3ErrorCode::NoError);
235 }
236}