h3/error/codes.rs
1//! HTTP/3 error codes.
2
3use std::fmt::{self};
4
5/// An HTTP/3 "application error code".
6#[derive(PartialEq, Eq, Hash, Clone, Copy)]
7pub struct Code {
8 code: u64,
9}
10
11impl Code {
12 /// Numerical error code
13 ///
14 /// See <https://www.rfc-editor.org/rfc/rfc9114.html#errors>
15 /// and <https://www.rfc-editor.org/rfc/rfc9000.html#error-codes>
16 pub const fn value(&self) -> u64 {
17 self.code
18 }
19}
20
21impl PartialEq<u64> for Code {
22 fn eq(&self, other: &u64) -> bool {
23 *other == self.code
24 }
25}
26
27// ===== impl Code =====
28
29macro_rules! codes {
30 (
31 $(
32 $(#[$docs:meta])*
33 ($num:expr, $name:ident);
34 )+
35 ) => {
36 impl Code {
37 $(
38 $(#[$docs])*
39 pub const $name: Code = Code{code: $num};
40 )+
41 }
42
43 impl fmt::Debug for Code {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 match self.code {
46 $(
47 $num => f.write_str(stringify!($name)),
48 )+
49 other => write!(f, "{:#x}", other),
50 }
51 }
52 }
53
54 impl fmt::Display for Code{
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self.code {
57 $(
58 $num => f.write_str(stringify!($name)),
59 )+
60 other => write!(f, "{:#x}", other),
61 }
62 }
63 }
64 }
65
66
67}
68
69codes! {
70 /// Datagram or capsule parse error
71 /// See: <https://www.rfc-editor.org/rfc/rfc9297#section-5.2>
72 (0x33, H3_DATAGRAM_ERROR);
73 /// No error. This is used when the connection or stream needs to be
74 /// closed, but there is no error to signal.
75 (0x100, H3_NO_ERROR);
76
77 /// Peer violated protocol requirements in a way that does not match a more
78 /// specific error code, or endpoint declines to use the more specific
79 /// error code.
80 (0x101, H3_GENERAL_PROTOCOL_ERROR);
81
82 /// An internal error has occurred in the HTTP stack.
83 (0x102, H3_INTERNAL_ERROR);
84
85 /// The endpoint detected that its peer created a stream that it will not
86 /// accept.
87 (0x103, H3_STREAM_CREATION_ERROR);
88
89 /// A stream required by the HTTP/3 connection was closed or reset.
90 (0x104, H3_CLOSED_CRITICAL_STREAM);
91
92 /// A frame was received that was not permitted in the current state or on
93 /// the current stream.
94 (0x105, H3_FRAME_UNEXPECTED);
95
96 /// A frame that fails to satisfy layout requirements or with an invalid
97 /// size was received.
98 (0x106, H3_FRAME_ERROR);
99
100 /// The endpoint detected that its peer is exhibiting a behavior that might
101 /// be generating excessive load.
102 (0x107, H3_EXCESSIVE_LOAD);
103
104 /// A Stream ID or Push ID was used incorrectly, such as exceeding a limit,
105 /// reducing a limit, or being reused.
106 (0x108, H3_ID_ERROR);
107
108 /// An endpoint detected an error in the payload of a SETTINGS frame.
109 (0x109, H3_SETTINGS_ERROR);
110
111 /// No SETTINGS frame was received at the beginning of the control stream.
112 (0x10a, H3_MISSING_SETTINGS);
113
114 /// A server rejected a request without performing any application
115 /// processing.
116 (0x10b, H3_REQUEST_REJECTED);
117
118 /// The request or its response (including pushed response) is cancelled.
119 (0x10c, H3_REQUEST_CANCELLED);
120
121 /// The client's stream terminated without containing a fully-formed
122 /// request.
123 (0x10d, H3_REQUEST_INCOMPLETE);
124
125 /// An HTTP message was malformed and cannot be processed.
126 (0x10e, H3_MESSAGE_ERROR);
127
128 /// The TCP connection established in response to a CONNECT request was
129 /// reset or abnormally closed.
130 (0x10f, H3_CONNECT_ERROR);
131
132 /// The requested operation cannot be served over HTTP/3. The peer should
133 /// retry over HTTP/1.1.
134 (0x110, H3_VERSION_FALLBACK);
135
136 /// The decoder failed to interpret an encoded field section and is not
137 /// able to continue decoding that field section.
138 (0x200, QPACK_DECOMPRESSION_FAILED);
139
140 /// The decoder failed to interpret an encoder instruction received on the
141 /// encoder stream.
142 (0x201, QPACK_ENCODER_STREAM_ERROR);
143
144 /// The encoder failed to interpret a decoder instruction received on the
145 /// decoder stream.
146 (0x202, QPACK_DECODER_STREAM_ERROR);
147}
148
149impl From<Code> for u64 {
150 fn from(code: Code) -> u64 {
151 code.code
152 }
153}
154
155impl From<u64> for Code {
156 fn from(code: u64) -> Code {
157 Code { code }
158 }
159}