use thiserror::Error;
#[derive(Debug, Error)]
pub enum DhtError {
#[error("transport error: {0}")]
Transport(String),
#[error("malformed response: {0}")]
MalformedResponse(String),
#[error("invalid hex identifier: {0}")]
InvalidHex(String),
#[error("no peers to query (routing table + bootstrap set are empty)")]
NoPeers,
#[error("rpc timed out")]
Timeout,
}
impl DhtError {
pub fn transport(e: impl std::fmt::Display) -> Self {
DhtError::Transport(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn transport_helper_formats() {
let e = DhtError::transport("connection refused");
assert!(e.to_string().contains("connection refused"));
assert!(matches!(e, DhtError::Transport(_)));
}
#[test]
fn error_messages_are_descriptive() {
assert!(DhtError::NoPeers.to_string().contains("no peers"));
assert!(DhtError::Timeout.to_string().contains("timed out"));
assert!(DhtError::MalformedResponse("x".into())
.to_string()
.contains("malformed"));
}
}