folk_protocol/error.rs
1//! Error type for the protocol crate.
2//!
3//! All fallible operations in `folk-protocol` return `Result<T, Error>`.
4
5use std::io;
6
7use thiserror::Error;
8
9/// Errors that can occur in `folk-protocol`.
10#[derive(Debug, Error)]
11pub enum Error {
12 /// The frame's length prefix exceeds the maximum allowed (16 MiB).
13 #[error("frame too large: {0} bytes (limit {limit})", limit = MAX_FRAME_SIZE)]
14 FrameTooLarge(usize),
15
16 /// The decoded payload is not a valid RPC message (e.g. wrong shape).
17 #[error("invalid frame: {0}")]
18 InvalidFrame(String),
19
20 /// `MessagePack` decoding failed.
21 #[error("decode error: {0}")]
22 Decode(#[from] rmp_serde::decode::Error),
23
24 /// `MessagePack` encoding failed.
25 #[error("encode error: {0}")]
26 Encode(#[from] rmp_serde::encode::Error),
27
28 /// Underlying I/O error from the framing layer.
29 #[error("io error: {0}")]
30 Io(#[from] io::Error),
31}
32
33/// Maximum allowed frame size, in bytes. Frames larger than this are rejected.
34pub const MAX_FRAME_SIZE: usize = 16 * 1024 * 1024;
35
36/// Convenience alias for `Result<T, Error>`.
37pub type Result<T> = std::result::Result<T, Error>;