naia_client/error.rs
1use std::{error::Error, fmt};
2
3/// Errors that can be returned by the naia client.
4///
5/// Returned by methods such as [`Client::send_message`] and the
6/// packet-processing loop when an unrecoverable transport or protocol
7/// condition is encountered.
8///
9/// [`Client::send_message`]: crate::Client::send_message
10#[derive(Debug)]
11pub enum NaiaClientError {
12 /// A general descriptive error message.
13 Message(String),
14 /// An error from an underlying layer, boxed for type erasure.
15 Wrapped(Box<dyn Error + Send>),
16 /// A packet could not be sent to the server.
17 SendError,
18 /// A packet could not be read from the socket.
19 RecvError,
20 /// A numeric entity or message identifier was malformed or out of range.
21 IdError(u16),
22 /// The target channel's send queue is at capacity. The message was not
23 /// queued. The caller may retry on the next tick or discard the message.
24 /// Configure [`ReliableSettings::max_queue_depth`] to adjust the limit.
25 MessageQueueFull,
26}
27
28impl NaiaClientError {
29 /// Constructs a `Message` variant from a string slice.
30 pub fn from_message(message: &str) -> Self {
31 Self::Message(message.to_string())
32 }
33}
34
35impl fmt::Display for NaiaClientError {
36 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
37 match self {
38 Self::Message(msg) => write!(f, "Naia Client Error: {}", msg),
39 Self::Wrapped(boxed_err) => fmt::Display::fmt(boxed_err.as_ref(), f),
40 Self::SendError => write!(f, "Naia Client Error: Send Error"),
41 Self::RecvError => write!(f, "Naia Client Error: Recv Error"),
42 Self::IdError(code) => write!(f, "Naia Client Error: Id Error: {}", code),
43 Self::MessageQueueFull => write!(f, "Naia Client Error: MessageQueueFull"),
44 }
45 }
46}
47
48impl Error for NaiaClientError {}
49// Safety: NaiaClientError::Wrapped requires Box<dyn Error + Send>, so the payload is Send.
50// The other variants contain only Copy/Clone primitive types. All variants are safe to send
51// across thread boundaries.
52unsafe impl Send for NaiaClientError {}
53// Safety: Same — all variant payloads are primitives or Send-bounded trait objects; no
54// interior mutability or thread-local state is involved.
55unsafe impl Sync for NaiaClientError {}