use thiserror::Error;
pub type Result<T> = std::result::Result<T, WireError>;
#[derive(Error, Debug)]
pub enum WireError {
#[error("Network error: {0}")]
Network(#[from] std::io::Error),
#[error("STUN error: {0}")]
Stun(String),
#[error("NAT traversal error: {0}")]
NatTraversal(String),
#[error("Peer not found: {0}")]
PeerNotFound(String),
#[error("Invalid message format: {0}")]
InvalidMessage(String),
#[error("Connection timeout")]
Timeout,
#[error("Address resolution error: {0}")]
AddressResolution(String),
#[error("Protocol version mismatch: expected {expected}, got {actual}")]
ProtocolVersionMismatch {
expected: u8,
actual: u8,
},
#[error("Cryptographic error: {0}")]
Crypto(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("URL parsing error: {0}")]
UrlParse(#[from] url::ParseError),
#[error("{0}")]
Other(String),
#[error("Bind error: {0}")]
BindError(String),
#[error("Connection error: {0}")]
ConnectionError(String),
#[error("Authentication error: {0}")]
AuthenticationError(String),
#[error("Resource exhausted: {0}")]
ResourceExhausted(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Not connected: {0}")]
NotConnected(String),
#[error("Channel error: {0}")]
ChannelError(String),
}
impl WireError {
pub fn stun<S: Into<String>>(msg: S) -> Self {
WireError::Stun(msg.into())
}
pub fn nat<S: Into<String>>(msg: S) -> Self {
WireError::NatTraversal(msg.into())
}
pub fn peer_not_found<S: Into<String>>(peer_id: S) -> Self {
WireError::PeerNotFound(peer_id.into())
}
pub fn invalid_message<S: Into<String>>(msg: S) -> Self {
WireError::InvalidMessage(msg.into())
}
pub fn address_resolution<S: Into<String>>(msg: S) -> Self {
WireError::AddressResolution(msg.into())
}
pub fn crypto<S: Into<String>>(msg: S) -> Self {
WireError::Crypto(msg.into())
}
pub fn other<S: Into<String>>(msg: S) -> Self {
WireError::Other(msg.into())
}
}