futures_yamux/
errors.rs

1use std::io;
2
3use thiserror::Error;
4
5/// Yamux errors type.
6#[derive(Debug, Error, PartialEq)]
7pub enum Error {
8    /// The operation cannot be completed because the connection is in an invalid state.
9    #[error("The operation cannot be completed because the connection is in an invalid state.")]
10    InvalidState,
11
12    /// The operation cannot be completed because the stream is in an invalid state.
13    /// The stream ID is provided as associated data.
14    #[error("The operation cannot be completed because the stream({0}) is in an invalid state.")]
15    InvalidStreamState(u32),
16
17    /// The peer violated the local flow control limits.
18    #[error("The peer violated the local flow control limits.")]
19    FlowControl,
20
21    /// The specified stream was reset by the peer.
22    ///
23    /// This flag is set by the received frame with the RST flag.
24    #[error("The specified stream({0}) was reset by the peer.")]
25    StreamReset(u32),
26
27    /// Call stream_send after setting the fin flag of the stream.
28    #[error("Call stream_send after setting the fin flag of the stream({0}).")]
29    FinalSize(u32),
30
31    #[error("There is no more work to do")]
32    Done,
33
34    /// The expected length is provided as associated data.
35    #[error("The provided buffer is too short. expected {0}")]
36    BufferTooShort(u32),
37
38    /// The length of the frame received was too long to handle.
39    #[error("The length of the frame received was too long to handle.")]
40    Overflow,
41
42    /// The provided frame cannot be parsed because its version is unknown.
43    ///
44    /// The frame version is provided as associated data.
45    #[error("The provided packet cannot be parsed because its version is unknown({0}).")]
46    UnknownVersion(u8),
47
48    /// The provide frame can't be parsed.
49    ///
50    /// The [`InvalidFrameKind`] is provided as associated data.
51    #[error("The provide frame can't be parsed,{0:?}")]
52    InvalidFrame(InvalidFrameKind),
53
54    /// Building frame violate the restrictions.
55    ///
56    /// The [`FrameRestrictionKind`] is provided as associated data.
57    #[error("Building frame violate the restrictions. {0:?}")]
58    FrameRestriction(FrameRestrictionKind),
59}
60
61/// Reason for not being able to parse the frame.
62#[derive(Debug, Clone, Copy, Error, PartialEq)]
63pub enum InvalidFrameKind {
64    #[error("Invalid frame type.")]
65    FrameType,
66
67    #[error("The Frame Flags field contains invalid flags.")]
68    Flags,
69    #[error("Data frame with empty body or Non data frame with non-empty body.")]
70    Body,
71    #[error("The GO_AWAY_FRAME and PING_FRAME should always use 0 StreamID, while other types of frames should not.")]
72    SessionId,
73}
74
75/// Reason for not being able to build the frame.
76#[derive(Debug, Clone, Copy, Error, PartialEq)]
77pub enum FrameRestrictionKind {
78    /// Only DATA_FRAME is allowed to set the body content.
79    #[error("Only DATA_FRAME is allowed to set the body content.")]
80    Body,
81    /// Set the invalid frame flags, see `create_without_body` for more information.
82    #[error("Set the invalid frame flags, see `create_without_body` for more information.")]
83    Flags,
84    /// The GO_AWAY_FRAME and PING_FRAME should always use 0 StreamID.
85    #[error("The GO_AWAY_FRAME and PING_FRAME should always use 0 StreamID, while other types of frames should not.")]
86    SessionId,
87}
88
89/// A specialized [`Result`](std::result::Result) type for yamux operations.
90pub type Result<T> = std::result::Result<T, Error>;
91
92impl From<Error> for io::Error {
93    fn from(value: Error) -> Self {
94        io::Error::new(io::ErrorKind::Other, value.to_string())
95    }
96}