Skip to content

Commit

Permalink
♻️ zb: use more appropriate address errors
Browse files Browse the repository at this point in the history
Use address::Error::MissingValue for missing address fields.

Use zbus::Error for errors that are not related to address
handling as such, but rather higher-level.
  • Loading branch information
elmarco committed Oct 8, 2024
1 parent 3c3a3ae commit c3341ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
14 changes: 9 additions & 5 deletions zbus/src/connection/connect/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::socket;
use crate::{
address::{transport::Transport, Address},
address::{self, transport::Transport, Address},
process::run,
Error, Result,
};
Expand All @@ -13,14 +13,15 @@ async fn launchd_bus_address(env_key: &str) -> Result<Address<'static>> {
.expect("failed to wait on launchctl output");

if !output.status.success() {
return Err(Error::Address(format!(
return Err(Error::Failure(format!(
"launchctl terminated with code: {}",
output.status
)));
}

let addr = String::from_utf8(output.stdout)
.map_err(|e| Error::Address(format!("Unable to parse launchctl output as UTF-8: {}", e)))?;
let addr = String::from_utf8(output.stdout).map_err(|e| {
address::Error::Encoding(format!("Unable to parse launchctl output as UTF-8: {}", e))
})?;

Ok(format!("unix:path={}", addr.trim()).try_into()?)
}
Expand All @@ -32,7 +33,10 @@ pub(crate) async fn connect(

match addr.transport()? {
Transport::Unix(t) => socket::unix::connect(&t).await,
_ => Err(Error::Address(format!("Address is unsupported: {}", addr))),
_ => {
tracing::debug!("Address is unsupported: {}", addr);
Err(Error::Unsupported)
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions zbus/src/connection/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) async fn connect_address(
}
}
}
Err(Error::Address("No connectable address".into()))
Err(Error::Failure("No connectable address".into()))
}

async fn connect(addr: &Address<'_>) -> ConnectResult {
Expand All @@ -50,7 +50,8 @@ async fn connect(addr: &Address<'_>) -> ConnectResult {
return win32::connect(&l).await;
}
_ => {
return Err(Error::Address(format!("Unhandled address: {}", addr)));
tracing::debug!("Unsupported address: {addr}");
return Err(Error::Unsupported);
}
};
Ok((split, guid))
Expand Down
24 changes: 13 additions & 11 deletions zbus/src/connection/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::os::fd::BorrowedFd;
#[cfg(not(feature = "tokio"))]
use std::{net::TcpStream, sync::Arc};

use crate::{address::transport::TcpFamily, Error, Result};
use crate::{
address::{self, transport::TcpFamily},
Result,
};

use super::{ReadHalf, RecvmsgResult, WriteHalf};
#[cfg(feature = "tokio")]
Expand Down Expand Up @@ -211,19 +214,18 @@ async fn connect_with(host: &str, port: u16, family: Option<TcpFamily>) -> Resul
},
"connect tcp",
)
.await
.map_err(|e| Error::Address(format!("Failed to receive TCP addresses: {e}")))?;
.await?;

// we could attempt connections in parallel?
let mut last_err = Error::Address("Failed to connect".into());
let mut last_err = io::Error::other("No address");
for addr in addrs {
match Stream::connect(addr).await {
Ok(stream) => return Ok(stream),
Err(e) => last_err = e.into(),
Err(e) => last_err = e,
}
}

Err(last_err)
Err(last_err.into())
}

#[cfg(feature = "tokio")]
Expand All @@ -238,10 +240,10 @@ async fn connect_with(host: &str, port: u16, family: Option<TcpFamily>) -> Resul

pub(crate) async fn connect(addr: &crate::address::transport::Tcp<'_>) -> Result<Stream> {
let Some(host) = addr.host() else {
return Err(Error::Address("No host in address".into()));
return Err(address::Error::MissingValue("host".into()).into());
};
let Some(port) = addr.port() else {
return Err(Error::Address("No port in address".into()));
return Err(address::Error::MissingValue("port".into()).into());
};

connect_with(host, port, addr.family()).await
Expand All @@ -251,13 +253,13 @@ pub(crate) async fn connect_nonce(
addr: &crate::address::transport::NonceTcp<'_>,
) -> Result<Stream> {
let Some(host) = addr.host() else {
return Err(Error::Address("No host in address".into()));
return Err(address::Error::MissingValue("host".into()).into());
};
let Some(port) = addr.port() else {
return Err(Error::Address("No port in address".into()));
return Err(address::Error::MissingValue("port".into()).into());
};
let Some(noncefile) = addr.noncefile() else {
return Err(Error::Address("No noncefile in address".into()));
return Err(address::Error::MissingValue("noncefile".into()).into());
};

#[allow(unused_mut)]
Expand Down
2 changes: 1 addition & 1 deletion zbus/src/connection/socket/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ pub(crate) async fn connect(addr: &crate::address::transport::Unix<'_>) -> Resul
UnixAddrKind::Path(p) => p.clone().into_owned(),
#[cfg(target_os = "linux")]
UnixAddrKind::Abstract(name) => SocketAddr::from_abstract_name(name)?,
_ => return Err(Error::Address("Address is not connectable".into())),
_ => return Err(Error::Failure("Address is not connectable".into())),
};

let stream = crate::Task::spawn_blocking(
Expand Down

0 comments on commit c3341ac

Please sign in to comment.