#[non_exhaustive]pub enum Error {
UnexpectedEof,
BufferFull,
VarintOverflow,
FrameTooLarge {
len: usize,
limit: usize,
},
InvalidLengthPrefix,
DelimiterNotFound,
InvalidEncoding,
BitOverflow,
EmptyDelimiter,
DelimiterInPayload,
ChecksumMismatch,
}Expand description
Errors returned by codec and framing operations.
The variants fall into three groups:
- Need more room or more data:
Error::UnexpectedEofandError::BufferFull. These are not protocol violations; a streaming caller can retry once more input has arrived or the output has been flushed. - Hostile or corrupt input:
Error::VarintOverflow,Error::FrameTooLarge,Error::InvalidLengthPrefix,Error::InvalidEncoding, andError::ChecksumMismatch. The peer sent bytes that violate the protocol. The usual response is to drop the connection or discard the datagram; resynchronising inside a corrupted stream is rarely possible. - Caller misuse:
Error::BitOverflow,Error::EmptyDelimiter, andError::DelimiterInPayload. The local program asked for something the codec cannot represent. Fix the call site.
§Example
use wire_codec::{Error, ReadBuf};
let mut buf = ReadBuf::new(&[0x01]);
// Only one byte available but we ask for two.
let result = buf.read_u16_be();
assert!(matches!(result, Err(Error::UnexpectedEof)));
assert_eq!(buf.position(), 0, "the cursor did not move");Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
UnexpectedEof
Reader ran out of bytes before the requested value could be decoded.
When decoding a stream, wait for more input and retry the same call. When decoding a complete message, the message is truncated.
BufferFull
Writer ran out of capacity before the requested value could be encoded.
Flush or grow the output buffer and retry. The cursor did not move, and the crate’s own writers check room before writing, so they leave the buffer untouched (see the crate-level error contract for the composite exceptions).
VarintOverflow
A varint exceeded the maximum encoded length for its target width, or its value does not fit in the target type.
The input is corrupt or hostile.
FrameTooLarge
A frame’s length exceeds the configured limit for the framer.
On decode, the peer announced (or, for delimited framing, has already
sent at least) len payload bytes against a limit of limit. Treat it
as hostile input. On encode, the local payload is larger than the
framer allows. Both fields saturate at usize::MAX.
Fields
InvalidLengthPrefix
A length prefix was structurally invalid: after applying the framer’s length adjustment it describes a negative payload length, or a payload cannot be represented under the configured adjustment.
DelimiterNotFound
A delimiter byte sequence was not found within the available input.
Not produced by the built-in framers, which report an over-long
undelimited input as Error::FrameTooLarge. Available to custom
Framer implementations.
InvalidEncoding
Decoded data violated a structural invariant required by the codec.
Produced by the canonical varint decoders and the varint-prefixed
framer for overlong encodings, by cobs decoding
for malformed blocks, by bool decoding for bytes other than 0 and
1, by packed structs whose padding bits are not zero, and by the
bytes codec when a custom framer breaks the
Frame contract. The input (or the
framer) is at fault.
BitOverflow
A bit-level write requested more bits than the target type can hold, or a value did not fit in the requested bit width.
EmptyDelimiter
A delimited-framer constructor was given an empty delimiter, which cannot uniquely separate frames.
DelimiterInPayload
A payload passed to a delimited framer’s write_frame contains the
delimiter sequence, so the receiver would split it into several frames.
Escape the payload (for example with cobs) or switch
to a length-prefixed framer. Nothing was written.
ChecksumMismatch
A frame’s checksum trailer did not match the checksum computed over the received bytes.
The frame was corrupted in transit. Discard it. A CRC does not
authenticate the sender; see checksum.
Trait Implementations§
impl Copy for Error
impl Eq for Error
Source§impl Error for Error
Implemented unconditionally, including on no_std targets.
std::error::Error is the same trait, so Error converts into
Box<dyn std::error::Error>, anyhow::Error, and friends whether or not
the std feature is enabled.
impl Error for Error
Implemented unconditionally, including on no_std targets.
std::error::Error is the same trait, so Error converts into
Box<dyn std::error::Error>, anyhow::Error, and friends whether or not
the std feature is enabled.
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<Error> for Error
Available on crate feature std only.Convert into an std::io::Error, for use in I/O-based codecs.
impl From<Error> for Error
std only.Convert into an std::io::Error, for use in I/O-based codecs.
The original Error is kept as the source and can be recovered with
io::Error::get_ref and downcast_ref. The std::io::ErrorKind is
chosen by group: Error::UnexpectedEof maps to UnexpectedEof,
Error::BufferFull to WriteZero, caller misuse
(Error::BitOverflow, Error::EmptyDelimiter,
Error::DelimiterInPayload) to InvalidInput, and everything else to
InvalidData.
§Example
use std::io;
use wire_codec::Error;
let io_err = io::Error::from(Error::ChecksumMismatch);
assert_eq!(io_err.kind(), io::ErrorKind::InvalidData);
let inner = io_err.get_ref().and_then(|e| e.downcast_ref::<Error>());
assert_eq!(inner, Some(&Error::ChecksumMismatch));