Skip to main content

ghast_client/
error.rs

1//! Errors and session outcomes.
2
3use ghast_protocol::{DecodeError, EncodeError, FrameError, State};
4use thiserror::Error;
5
6/// How a session that reached the play state ended without an error.
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum SessionEnd {
10    /// The server kicked the player.
11    Kicked {
12        /// The reason as plain text.
13        reason: String,
14        /// The reason as the server sent it (chat JSON).
15        json: String,
16    },
17    /// The server closed the connection without a reason.
18    ServerClosed,
19    /// The behaviour called [`crate::TickContext::disconnect`].
20    Quit(String),
21    /// The shutdown future given to [`crate::Client::run_until`] completed.
22    Shutdown,
23    /// The server sent the client to another server (1.20.5+ `transfer`, from the
24    /// configuration or the play state) and [`crate::Client::follow_transfers`] is off (or the
25    /// transfer limit was reached). Vanilla connects there with the transfer intent.
26    Transferred {
27        /// Target host.
28        host: String,
29        /// Target port.
30        port: u16,
31    },
32}
33
34/// A session failed.
35#[derive(Debug, Error)]
36#[non_exhaustive]
37pub enum ClientError {
38    /// Opening the connection (direct or through the proxy) failed.
39    #[error(transparent)]
40    Connect(#[from] ghast_auth::connect::ConnectError),
41    /// The username is not valid for an offline login.
42    #[error("invalid username: {0}")]
43    Username(#[from] ghast_auth::offline::UsernameError),
44    /// Socket error.
45    #[error("connection error: {0}")]
46    Io(#[from] std::io::Error),
47    /// Framing / compression / encryption error (the stream is unusable).
48    #[error("bad frame from the server: {0}")]
49    Frame(#[source] FrameError),
50    /// A packet the client needs could not be decoded.
51    #[error("could not decode {state:?} packet 0x{id:02x}: {source}")]
52    Decode {
53        /// Connection state.
54        state: State,
55        /// Packet id.
56        id: i32,
57        /// What was wrong.
58        #[source]
59        source: DecodeError,
60    },
61    /// Chunk data from the server was malformed. Since 0.1.3 a session no longer ends with
62    /// it: the column is dropped, the error logged and play goes on (like vanilla). Kept for
63    /// compatibility.
64    #[error("bad chunk data: {0}")]
65    World(#[source] ghast_world::WorldError),
66    /// A packet could not be encoded (a value the protocol cannot carry).
67    #[error("could not encode a packet: {0}")]
68    Encode(#[from] EncodeError),
69    /// The server refused the login.
70    #[error("login refused: {0}")]
71    LoginRefused(String),
72    /// The server closed the connection during login.
73    #[error("the server closed the connection during login")]
74    LoginClosed,
75    /// The server is in online mode but the account is an offline one.
76    #[error("the server requires online mode (a Microsoft account)")]
77    OnlineModeRequired,
78    /// The encryption handshake failed.
79    #[error("encryption handshake: {0}")]
80    Crypto(#[from] ghast_auth::crypto::CryptoError),
81    /// The session server refused the join (online mode).
82    #[cfg(feature = "microsoft")]
83    #[error("session server: {0}")]
84    Session(#[from] ghast_auth::microsoft::MicrosoftError),
85    /// No data from the server for too long.
86    #[error("timed out: {0}")]
87    Timeout(&'static str),
88    /// The version's cargo feature is off in this build of the library.
89    #[error("Minecraft {0} is not compiled in (enable the ghast feature for it)")]
90    VersionNotEnabled(crate::GameVersion),
91}
92
93impl From<FrameError> for ClientError {
94    fn from(e: FrameError) -> Self {
95        match e {
96            FrameError::Io(io) => ClientError::Io(io),
97            e => ClientError::Frame(e),
98        }
99    }
100}