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