hoy_protocol/error.rs
1//! Hoy-protocol error definitions.
2
3use thiserror::Error;
4
5/// Errors produced by the Hoy protocol codec.
6#[derive(Debug, Error)]
7pub enum ProtocolError {
8 /// The serialized payload length does not fit into a `u32` length prefix.
9 #[error("Frame too large: {size} bytes")]
10 FrameTooLarge {
11 /// Payload size in bytes.
12 size: usize,
13 },
14
15 /// The provided frame contains less bytes than declared in its header.
16 #[error("Truncated frame")]
17 TruncatedFrame,
18
19 /// The provided frame header is malformed.
20 #[error("Invalid frame length prefix")]
21 InvalidLengthPrefix,
22
23 /// The provided frame length cannot be represented by the current platform.
24 #[error("Frame length cannot be represented on this platform: {length}")]
25 FrameLengthOutOfRange {
26 /// Raw frame length decoded from the wire.
27 length: u32,
28 },
29
30 /// Failed to compute a valid buffer capacity for a frame.
31 #[error("Frame capacity overflow")]
32 CapacityOverflow,
33
34 /// Attempt to discard an invalid number of bytes from a frame buffer.
35 #[error("Attempted to discard {count}B from buffer of length {buffer_len}")]
36 InvalidDiscard {
37 /// Requested discard size.
38 count: usize,
39
40 /// Current buffer length.
41 buffer_len: usize,
42 },
43
44 /// Serialization or deserialization error.
45 #[error("Serialization error: {0}")]
46 Serde(#[from] serde_json::Error),
47}