aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/resolver.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-08-06 07:22:19 +0000
committerGitHub <noreply@github.com>2022-08-06 02:22:19 -0500
commit5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch)
treeb006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-protocol/src/resolver.rs
parent1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff)
downloadazalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz
Better errors (#14)
* make reading use thiserror * finish implementing all the error things * clippy warnings related to ok_or * fix some errors in other places * thiserror in more places * don't use closures in a couple places * errors in writing packet * rip backtraces * change some BufReadError::Custom to UnexpectedEnumVariant * Errors say what packet is bad * error on leftover data and fix it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-protocol/src/resolver.rs')
-rwxr-xr-xazalea-protocol/src/resolver.rs20
1 files changed, 14 insertions, 6 deletions
diff --git a/azalea-protocol/src/resolver.rs b/azalea-protocol/src/resolver.rs
index 24687a6e..e21362a5 100755
--- a/azalea-protocol/src/resolver.rs
+++ b/azalea-protocol/src/resolver.rs
@@ -1,16 +1,24 @@
-use std::net::IpAddr;
-
use crate::{ServerAddress, ServerIpAddress};
use async_recursion::async_recursion;
+use std::net::IpAddr;
+use thiserror::Error;
use trust_dns_resolver::{
config::{ResolverConfig, ResolverOpts},
TokioAsyncResolver,
};
+#[derive(Error, Debug)]
+pub enum ResolverError {
+ #[error("No SRV record found")]
+ NoSrvRecord,
+ #[error("No IP found")]
+ NoIp,
+}
+
/// Resolve a Minecraft server address into an IP address and port.
/// If it's already an IP address, it's returned as-is.
#[async_recursion]
-pub async fn resolve_address(address: &ServerAddress) -> Result<ServerIpAddress, String> {
+pub async fn resolve_address(address: &ServerAddress) -> Result<ServerIpAddress, ResolverError> {
// If the address.host is already in the format of an ip address, return it.
if let Ok(ip) = address.host.parse::<IpAddr>() {
return Ok(ServerIpAddress {
@@ -33,20 +41,20 @@ pub async fn resolve_address(address: &ServerAddress) -> Result<ServerIpAddress,
let redirect_srv = redirect_result
.iter()
.next()
- .ok_or_else(|| "No SRV record found".to_string())?;
+ .ok_or(ResolverError::NoSrvRecord)?;
let redirect_address = ServerAddress {
host: redirect_srv.target().to_utf8(),
port: redirect_srv.port(),
};
- println!("redirecting to {:?}", redirect_address);
+ // println!("redirecting to {:?}", redirect_address);
return resolve_address(&redirect_address).await;
}
// there's no redirect, try to resolve this as an ip address
let lookup_ip_result = resolver.lookup_ip(address.host.clone()).await;
- let lookup_ip = lookup_ip_result.map_err(|_| "No IP found".to_string())?;
+ let lookup_ip = lookup_ip_result.map_err(|_| ResolverError::NoIp)?;
Ok(ServerIpAddress {
ip: lookup_ip.iter().next().unwrap(),