Skip to content

Commit

Permalink
docs: update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Mar 8, 2024
1 parent c084262 commit 7875f1d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
15 changes: 10 additions & 5 deletions src/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl From<BedrockServerInfo> for crate::JavaServerInfo {
}
}

/// Server MOTD string is missing information.
#[derive(Debug, Snafu)]
#[snafu(display("Not enough motd components"))]
pub struct ServerInfoParseError;

impl FromStr for BedrockServerInfo {
Expand Down Expand Up @@ -88,30 +88,35 @@ impl FromStr for BedrockServerInfo {

#[derive(Debug, Snafu)]
pub enum BedrockPingError {
/// Failed to parse address.
#[snafu(display("Failed to parse address {address:?}: {source}"))]
AddressParse {
source: AddrParseError,
address: String,
backtrace: Backtrace,
},
#[snafu(display("Server did not respond"))]
/// The server did not respond to the ping request.
NoResponse { backtrace: Backtrace },
/// Failed to parse server info.
#[snafu(display("Failed to parse server info: {source}"), context(false))]
ServerInfoParse {
source: ServerInfoParseError,
backtrace: Backtrace,
},
#[snafu(display("io error: {source}"), context(false))]
/// I/O error.
#[snafu(display("I/O error: {source}"), context(false))]
Io {
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("dns lookup failed for address `{address}`"))]
/// DNS lookup failed.
#[snafu(display("DNS lookup failed for address `{address}`"))]
DNSLookupFailed {
address: String,
backtrace: Backtrace,
},
#[snafu(display("failed to open socket: {source}"))]
/// Failed to open socket.
#[snafu(display("Failed to open socket: {source}"))]
ConnectFailed {
source: std::io::Error,
backtrace: Backtrace,
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ pub mod bedrock;
#[cfg(feature = "simple")]
#[derive(Snafu, Debug)]
pub enum PingError {
#[snafu(display("connection failed: {source}"), context(false))]
/// Connection failed.
#[snafu(display("Connection failed: {source}"), context(false))]
Protocol {
#[snafu(backtrace)]
source: crate::protocol::ProtocolError,
},
#[snafu(display("connection did not respond in time"))]
/// The connection did not finish in time.
Timeout { backtrace: Backtrace },
}

Expand Down
27 changes: 15 additions & 12 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,44 @@ mod frame;

#[derive(Snafu, Debug)]
pub enum ProtocolError {
#[snafu(display("io error: {source}"), context(false))]
/// I/O error.
#[snafu(display("I/O error: {source}"), context(false))]
Io {
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("failed to encode string as bytes: {source}"), context(false))]
/// Failed to encode string as bytes.
#[snafu(display("Failed to encode string as bytes: {source}"), context(false))]
StringEncodeFailed {
#[snafu(backtrace)]
source: McStringError,
},
#[snafu(display(
"failed to send packet because it is too long (more than {} bytes)",
i32::MAX
))]
/// Failed to send a packet because it was longer than the maximum i32.
PacketTooLong { backtrace: Backtrace },
#[snafu(display("connection closed unexpectedly"))]
/// Connection closed unexpectedly.
ConnectionClosed { backtrace: Backtrace },
#[snafu(display("failed to parse packet: {source}"), context(false))]
/// Failed to parse a packet.
#[snafu(display("Failed to parse a packet: {source}"), context(false))]
ParseFailed {
#[snafu(backtrace)]
source: FrameError,
},
#[snafu(display("srv resolver creation failed: {source}"), context(false))]
/// Failed to resolve SRV record.
#[snafu(display("Failed to resolve SRV record: {source}"), context(false))]
SrvResolveError {
source: trust_dns_resolver::error::ResolveError,
backtrace: Backtrace,
},
#[snafu(display("packet received out of order"))]
/// Packet received out of order.
FrameOutOfOrder { backtrace: Backtrace },
#[snafu(display("failed to parse server response: {source}"), context(false))]
/// Failed to parse JSON response.
#[snafu(display("Failed to parse JSON response: {source}"), context(false))]
JsonParse {
source: serde_json::Error,
backtrace: Backtrace,
},
#[snafu(display("dns lookup failed for address `{address}`"))]
/// DNS lookup failed.
#[snafu(display("DNS lookup failed for address `{address}`."))]
DNSLookupFailed {
address: String,
backtrace: Backtrace,
Expand Down
24 changes: 9 additions & 15 deletions src/protocol/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,24 @@ use crate::mc_string::{decode_mc_string, McStringError};

#[derive(Snafu, Debug)]
pub enum FrameError {
#[snafu(display("frame is missing data"))]
/// Received an incomplete frame.
Incomplete { backtrace: Backtrace },
#[snafu(display("io error: {source}"), context(false))]
/// I/O error.
#[snafu(display("I/O error: {source}"), context(false))]
Io {
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display("frame declares it has negative length"))]
/// Received a frame with an invalid length.
InvalidLength { backtrace: Backtrace },
#[snafu(display("cannot parse frame with id {id}"))]
InvalidFrame { id: i32, backtrace: Backtrace },
#[snafu(display("failed to decode string: {source}"), context(false))]
/// Received a frame with an invalid id.
InvalidFrameId { id: i32, backtrace: Backtrace },
/// Failed to decode string.
#[snafu(display("Failed to decode string: {source}"), context(false))]
StringDecodeFailed {
#[snafu(backtrace)]
source: McStringError,
},
#[snafu(
display("failed to decode ping response payload: {source}"),
context(false)
)]
PingResponseDecodeFailed {
source: TryFromSliceError,
backtrace: Backtrace,
},
}

#[derive(Debug)]
Expand Down Expand Up @@ -155,6 +149,6 @@ impl Frame {
}
}

InvalidFrameSnafu { id }.fail()
InvalidFrameIdSnafu { id }.fail()
}
}

0 comments on commit 7875f1d

Please sign in to comment.