nv_kcp/
error.rs

1use std::{
2    error::Error as StdError,
3    io::{self, ErrorKind},
4};
5
6/// KCP protocol errors
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    #[error("conv inconsistent, expected {0}, found {1}")]
10    ConvInconsistent(u32, u32),
11    #[error("invalid mtu {0}")]
12    InvalidMtu(usize),
13    #[error("invalid segment size {0}")]
14    InvalidSegmentSize(usize),
15    #[error("invalid segment data size, expected {0}, found {1}")]
16    InvalidSegmentDataSize(usize, usize),
17    #[error("{0}")]
18    IoError(
19        #[from]
20        #[source]
21        io::Error,
22    ),
23    #[error("need to call update() once")]
24    NeedUpdate,
25    #[error("recv queue is empty")]
26    RecvQueueEmpty,
27    #[error("expecting fragment")]
28    ExpectingFragment,
29    #[error("command {0} is not supported")]
30    UnsupportedCmd(u8),
31    #[error("user's send buffer is too big")]
32    UserBufTooBig,
33    #[error("user's recv buffer is too small")]
34    UserBufTooSmall,
35    #[error("token mismatch, expected {0}, found {1}")]
36    TokenMismatch(u32, u32),
37}
38
39fn make_io_error<T>(kind: ErrorKind, msg: T) -> io::Error
40where
41    T: Into<Box<dyn StdError + Send + Sync>>,
42{
43    io::Error::new(kind, msg)
44}
45
46impl From<Error> for io::Error {
47    fn from(err: Error) -> Self {
48        let kind = match err {
49            Error::IoError(err) => return err,
50            Error::RecvQueueEmpty | Error::ExpectingFragment => ErrorKind::WouldBlock,
51            _ => ErrorKind::Other,
52        };
53
54        make_io_error(kind, err)
55    }
56}