kcp_io/core/error.rs
1//! Error types for KCP operations.
2//!
3//! Defines [`KcpError`] which covers all failure modes of the KCP protocol
4//! engine, and [`KcpResult<T>`] as a convenient type alias.
5
6use thiserror::Error;
7
8/// Error type for KCP protocol operations.
9///
10/// Each variant maps to a specific failure condition when interacting with
11/// the underlying KCP C library through the safe [`Kcp`](super::Kcp) wrapper.
12#[derive(Debug, Error)]
13pub enum KcpError {
14 /// Failed to create a KCP instance (e.g., `ikcp_create` returned null).
15 #[error("failed to create KCP instance")]
16 CreateFailed,
17 /// Failed to send data through KCP. The inner value is the C error code.
18 #[error("send failed (error code: {0})")]
19 SendFailed(i32),
20 /// No data is available to receive right now. This is not a fatal error;
21 /// the caller should retry after feeding more input data and calling `update()`.
22 #[error("no data available to receive (would block)")]
23 RecvWouldBlock,
24 /// The provided receive buffer is too small for the next message.
25 #[error("receive buffer too small (need {need} bytes, got {got} bytes)")]
26 RecvBufferTooSmall {
27 /// The size of the next available message.
28 need: usize,
29 /// The size of the buffer that was provided.
30 got: usize,
31 },
32 /// A generic receive failure with the C error code.
33 #[error("recv failed (error code: {0})")]
34 RecvFailed(i32),
35 /// Failed to feed input data to the KCP engine (e.g., corrupted packet).
36 #[error("input failed (error code: {0})")]
37 InputFailed(i32),
38 /// Failed to set the MTU to the specified value.
39 #[error("failed to set MTU to {mtu} (error code: {code})")]
40 SetMtuFailed {
41 /// The MTU value that was attempted.
42 mtu: u32,
43 /// The C error code returned.
44 code: i32,
45 },
46 /// An invalid configuration parameter was provided.
47 #[error("invalid configuration: {0}")]
48 InvalidConfig(String),
49 /// The conversation ID in the received packet does not match the expected one.
50 #[error("conversation ID mismatch (expected {expected}, got {got})")]
51 ConvMismatch {
52 /// The expected conversation ID.
53 expected: u32,
54 /// The conversation ID found in the packet.
55 got: u32,
56 },
57 /// An I/O error occurred in the output callback.
58 #[error("output callback I/O error: {0}")]
59 OutputError(#[from] std::io::Error),
60}
61
62/// A convenience type alias for `Result<T, KcpError>`.
63pub type KcpResult<T> = Result<T, KcpError>;