use thiserror::Error;
#[derive(Error, Debug)]
pub enum McError {
#[error(transparent)]
Network(#[from] NetworkError),
#[error(transparent)]
Protocol(#[from] ProtocolError),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
Config(#[from] ConfigError),
#[cfg(feature = "proxy")]
#[error(transparent)]
Proxy(#[from] ProxyError),
}
#[derive(Error, Debug)]
pub enum NetworkError {
#[error("DNS resolution failed: {0}")]
Dns(String),
#[error("Connection failed: {0}")]
Connection(String),
#[error("Request timed out")]
Timeout,
}
#[derive(Error, Debug)]
pub enum ProtocolError {
#[error("Invalid server response: {0}")]
InvalidResponse(String),
#[error("JSON parsing error: {0}")]
Json(#[from] serde_json::Error),
#[error("UTF-8 conversion error: {0}")]
Utf8(#[from] std::string::FromUtf8Error),
#[error("Base64 decoding error: {0}")]
Base64(#[from] base64::DecodeError),
}
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("Invalid edition: {0}")]
InvalidEdition(String),
#[error("Invalid port: {0}")]
InvalidPort(String),
#[error("Invalid address format: {0}")]
InvalidAddress(String),
}
#[cfg(feature = "proxy")]
#[derive(Error, Debug)]
pub enum ProxyError {
#[error("Proxy error: {0}")]
Connection(String),
#[error("Proxy '{0}' does not support UDP — Bedrock pings require UDP ASSOCIATE")]
UdpUnsupported(String),
}
impl McError {
#[doc(hidden)]
pub fn dns(msg: impl Into<String>) -> Self {
NetworkError::Dns(msg.into()).into()
}
#[doc(hidden)]
pub fn connection(msg: impl Into<String>) -> Self {
NetworkError::Connection(msg.into()).into()
}
#[doc(hidden)]
pub fn timeout() -> Self {
NetworkError::Timeout.into()
}
#[doc(hidden)]
pub fn invalid_response(msg: impl Into<String>) -> Self {
ProtocolError::InvalidResponse(msg.into()).into()
}
#[doc(hidden)]
pub fn invalid_port(msg: impl Into<String>) -> Self {
ConfigError::InvalidPort(msg.into()).into()
}
#[doc(hidden)]
pub fn invalid_address(msg: impl Into<String>) -> Self {
ConfigError::InvalidAddress(msg.into()).into()
}
#[doc(hidden)]
pub fn invalid_edition(msg: impl Into<String>) -> Self {
ConfigError::InvalidEdition(msg.into()).into()
}
#[cfg(feature = "proxy")]
#[doc(hidden)]
pub fn proxy_error(msg: impl Into<String>) -> Self {
ProxyError::Connection(msg.into()).into()
}
#[cfg(feature = "proxy")]
#[doc(hidden)]
pub fn proxy_udp_unsupported(addr: impl Into<String>) -> Self {
ProxyError::UdpUnsupported(addr.into()).into()
}
}