renetcode2/
error.rs

1use std::{error, fmt, io};
2
3use crate::{token::TokenGenerationError, DisconnectReason, NETCODE_MAX_PAYLOAD_BYTES};
4use chacha20poly1305::aead::Error as CryptoError;
5
6/// Errors from the renetcode crate.
7#[derive(Debug)]
8pub enum NetcodeError {
9    /// No private keys was available while decrypting.
10    UnavailablePrivateKey,
11    /// The type of the packet is invalid.
12    InvalidPacketType,
13    /// The connect token has an invalid protocol id.
14    InvalidProtocolID,
15    /// The connect token has an invalid version.
16    InvalidVersion,
17    /// The connect token was transmitted by an invalid socket.
18    InvalidSocketId,
19    /// Packet size is too small to be a netcode packet.
20    PacketTooSmall,
21    /// Payload is above the maximum limit
22    PayloadAboveLimit,
23    /// The processed packet is duplicated
24    DuplicatedSequence,
25    /// No more host are available in the connect token..
26    NoMoreServers,
27    /// The connect token has expired.
28    Expired,
29    /// The client is disconnected.
30    Disconnected(DisconnectReason),
31    /// An error occurred while encrypting or decrypting.
32    CryptoError,
33    /// The server address is not in the connect token.
34    NotInHostList,
35    /// Client was not found.
36    ClientNotFound,
37    /// Client is not connected.
38    ClientNotConnected,
39    /// IO error.
40    IoError(io::Error),
41    /// An error occurred while generating the connect token.
42    TokenGenerationError(TokenGenerationError),
43}
44
45impl fmt::Display for NetcodeError {
46    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
47        use NetcodeError::*;
48
49        match *self {
50            UnavailablePrivateKey => write!(fmt, "no private key was found for this address"),
51            InvalidPacketType => write!(fmt, "invalid packet type"),
52            InvalidProtocolID => write!(fmt, "invalid protocol id"),
53            InvalidSocketId => write!(fmt, "invalid socket id"),
54            InvalidVersion => write!(fmt, "invalid version info"),
55            PacketTooSmall => write!(fmt, "packet is too small"),
56            PayloadAboveLimit => write!(fmt, "payload is above the {} bytes limit", NETCODE_MAX_PAYLOAD_BYTES),
57            Expired => write!(fmt, "connection expired"),
58            DuplicatedSequence => write!(fmt, "sequence already received"),
59            Disconnected(reason) => write!(fmt, "disconnected: {}", reason),
60            NoMoreServers => write!(fmt, "client has no more servers to connect"),
61            CryptoError => write!(fmt, "error while encoding or decoding"),
62            NotInHostList => write!(fmt, "token does not contain the server address"),
63            ClientNotFound => write!(fmt, "client was not found"),
64            ClientNotConnected => write!(fmt, "client is disconnected or connecting"),
65            IoError(ref err) => write!(fmt, "{}", err),
66            TokenGenerationError(ref err) => write!(fmt, "{}", err),
67        }
68    }
69}
70
71impl error::Error for NetcodeError {}
72
73impl From<io::Error> for NetcodeError {
74    fn from(inner: io::Error) -> Self {
75        NetcodeError::IoError(inner)
76    }
77}
78
79impl From<TokenGenerationError> for NetcodeError {
80    fn from(inner: TokenGenerationError) -> Self {
81        NetcodeError::TokenGenerationError(inner)
82    }
83}
84
85impl From<CryptoError> for NetcodeError {
86    fn from(_: CryptoError) -> Self {
87        NetcodeError::CryptoError
88    }
89}