1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use std::convert::Infallible; 15 use std::error::Error; 16 use std::fmt::{Debug, Display, Formatter}; 17 18 use crate::h3::qpack::error::QpackError; 19 20 /// HTTP3 errors. 21 #[derive(Debug, Eq, PartialEq, Clone)] 22 pub enum H3Error { 23 /// Serialization error. 24 Encode(EncodeError), 25 /// Deserialization error. 26 Decode(DecodeError), 27 /// Common error during serialization or deserialization. 28 Serialize(CommonError), 29 /// Connection level error. 30 Connection(H3ErrorCode), 31 /// Stream level error. 32 Stream(u64, H3ErrorCode), 33 } 34 35 /// Error during serialization. 36 #[derive(Debug, Eq, PartialEq, Clone)] 37 pub enum EncodeError { 38 /// The set frame could not be found during serialization. 39 NoCurrentFrame, 40 /// The type of frame set does not match the serialized one. 41 WrongTypeFrame, 42 /// The previous frame has not been serialized. 43 RepeatSetFrame, 44 /// Sets a frame of unknown type. 45 UnknownFrameType, 46 /// Too many additional Settings are encoded. 47 TooManySettings, 48 /// qpack encoder encoding error. 49 QpackError(QpackError), 50 } 51 52 /// Error during deserialization. 53 #[derive(Debug, Eq, PartialEq, Clone)] 54 pub enum DecodeError { 55 /// The frame type does not correspond to the stream type. 56 UnexpectedFrame(u64), 57 /// Qpack decoder decoding error. 58 QpackError(QpackError), 59 /// The payload length resolved is different from the actual data. 60 FrameSizeError(u64), 61 /// Http3 does not allow the type of setting. 62 UnsupportedSetting(u64), 63 } 64 65 /// Errors during serialization and deserialization, 66 /// usually occur during variable interger serialization and deserialization. 67 #[derive(Debug, Eq, PartialEq, Clone)] 68 pub enum CommonError { 69 /// The buf used to store serialized data is too short. 70 BufferTooShort, 71 /// The field for the frame is missing. 72 FieldMissing, 73 /// Computation time overflow. 74 CalculateOverflow, 75 /// Internal error. 76 InternalError, 77 } 78 79 /// Common http3 error codes defined in the rfc documentation. 80 /// Refers to [`iana`]. 81 /// 82 /// [`iana`]: https://www.iana.org/assignments/http3-parameters/http3-parameters.xhtml#http3-parameters-error-codes 83 #[derive(Debug, Eq, PartialEq, Copy, Clone)] 84 pub enum H3ErrorCode { 85 /// Datagram or Capsule Protocol parse error. 86 H3DatagramError = 0x33, 87 /// No error. 88 H3NoError = 0x100, 89 /// General protocol error. 90 H3GeneralProtocolError = 0x101, 91 /// Internal error. 92 H3InternalError = 0x102, 93 /// Stream creation error. 94 H3StreamCreationError = 0x103, 95 /// Critical stream was closed. 96 H3ClosedCriticalStream = 0x104, 97 /// Frame not permitted in the current state. 98 H3FrameUnexpected = 0x105, 99 /// Frame violated layout or size rules. 100 H3FrameError = 0x106, 101 /// Peer generating excessive load. 102 H3ExcessiveLoad = 0x107, 103 /// An identifier was used incorrectly. 104 H3IdError = 0x108, 105 /// SETTINGS frame contained invalid values. 106 H3SettingsError = 0x109, 107 /// No SETTINGS frame received. 108 H3MissingSettings = 0x10A, 109 /// Request not processed. 110 H3RequestRejected = 0x10B, 111 /// Data no longer needed. 112 H3RequestCancelled = 0x10C, 113 /// Stream terminated early. 114 H3RequestIncomplete = 0x10D, 115 /// Malformed message. 116 H3MessageError = 0x10E, 117 /// TCP reset or error on CONNECT request. 118 H3ConnectError = 0x10F, 119 /// Retry over HTTP/1.1. 120 H3VersionFallback = 0x110, 121 /// Decoding of a field section failed. 122 QPACKDecompressionFailed = 0x200, 123 /// Error on the encoder stream. 124 QPACKEncoderStreamError = 0x201, 125 /// Error on the decoder stream. 126 QPACKDecoderStreamError = 0x202, 127 } 128 129 impl From<u64> for H3ErrorCode { from(value: u64) -> Self130 fn from(value: u64) -> Self { 131 match value { 132 0x33 => H3ErrorCode::H3DatagramError, 133 0x100 => H3ErrorCode::H3NoError, 134 0x101 => H3ErrorCode::H3GeneralProtocolError, 135 0x102 => H3ErrorCode::H3InternalError, 136 0x103 => H3ErrorCode::H3StreamCreationError, 137 0x104 => H3ErrorCode::H3ClosedCriticalStream, 138 0x105 => H3ErrorCode::H3FrameUnexpected, 139 0x106 => H3ErrorCode::H3FrameError, 140 0x107 => H3ErrorCode::H3ExcessiveLoad, 141 0x108 => H3ErrorCode::H3IdError, 142 0x109 => H3ErrorCode::H3SettingsError, 143 0x10A => H3ErrorCode::H3MissingSettings, 144 0x10B => H3ErrorCode::H3RequestRejected, 145 0x10C => H3ErrorCode::H3RequestCancelled, 146 0x10D => H3ErrorCode::H3RequestIncomplete, 147 0x10E => H3ErrorCode::H3MessageError, 148 0x10F => H3ErrorCode::H3ConnectError, 149 0x110 => H3ErrorCode::H3VersionFallback, 150 0x200 => H3ErrorCode::QPACKDecompressionFailed, 151 0x201 => H3ErrorCode::QPACKEncoderStreamError, 152 0x202 => H3ErrorCode::QPACKDecoderStreamError, 153 _ => H3ErrorCode::H3GeneralProtocolError, 154 } 155 } 156 } 157 158 impl From<QpackError> for DecodeError { from(value: QpackError) -> Self159 fn from(value: QpackError) -> Self { 160 DecodeError::QpackError(value) 161 } 162 } 163 164 impl From<QpackError> for EncodeError { from(value: QpackError) -> Self165 fn from(value: QpackError) -> Self { 166 EncodeError::QpackError(value) 167 } 168 } 169 170 impl From<EncodeError> for H3Error { from(value: EncodeError) -> Self171 fn from(value: EncodeError) -> Self { 172 H3Error::Encode(value) 173 } 174 } 175 176 impl From<DecodeError> for H3Error { from(value: DecodeError) -> Self177 fn from(value: DecodeError) -> Self { 178 H3Error::Decode(value) 179 } 180 } 181 182 impl From<CommonError> for H3Error { from(value: CommonError) -> Self183 fn from(value: CommonError) -> Self { 184 H3Error::Serialize(value) 185 } 186 } 187 188 impl From<Infallible> for H3Error { from(_value: Infallible) -> Self189 fn from(_value: Infallible) -> Self { 190 unreachable!() 191 } 192 } 193 194 impl Display for H3Error { fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result195 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { 196 Debug::fmt(self, f) 197 } 198 } 199 200 impl Error for H3Error {} 201