Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion zbus/src/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,16 @@ impl<'a> Builder<'a> {
Target::VsockStream(stream) => stream.into(),
Target::Address(address) => {
guid = address.guid().map(|g| g.to_owned().into());
match address.connect().await? {

match address
.clone() //This clone is necessary to capture on error the address because the connect
// function consume the address object.
Comment on lines +530 to +531
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have the Address::connect do the mapping instead, or return Result<Stream, (io::Error, Self) (open to other options too) or maybe even have the Address::connect just not take ownership of address instance (it's internal after all).

Since this should not require a major effort and there is no urgency here, I think it makes sense to already avoid the cloning here, instead of leaving it for the future, which is likely never going to happen. :)

P.S. do remember the atomic commits rule, so please put the needed refactoring in a separate commit that comes before this one. 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have the Address::connect do the mapping instead, or return Result<Stream, (io::Error, Self) (open to other options too) or maybe even have the Address::connect just not take ownership of address instance (it's internal after all).

Just FYI, I would very much prefer the first option here: have the Address::connect (or Transport::connect) do the mapping.

.connect()
.await
.map_err(|error| match error {
Error::InputOutput(error) => Error::InputOutputAddress(error, address),
_ => error,
})? {
#[cfg(any(unix, not(feature = "tokio")))]
address::transport::Stream::Unix(stream) => stream.into(),
#[cfg(unix)]
Expand Down
8 changes: 8 additions & 0 deletions zbus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use zvariant::{Error as VariantError, ObjectPath};
use crate::{
fdo,
message::{Message, Type},
Address,
};

/// The error type for `zbus`.
Expand All @@ -20,6 +21,8 @@ pub enum Error {
Address(String),
/// An I/O error.
InputOutput(Arc<io::Error>),
/// An I/O error with the related URI.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be a bit more descriptive here and document the arguments (especially since we don't name them in here, which I know is not ideal).

InputOutputAddress(Arc<io::Error>, Address),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a very strange name IMO that will confuse people. I actually suggested a specific name in #14778: zbus::Error::Connection. This error is about connection failures.

/// Invalid message field.
InvalidField,
/// Data too large.
Expand Down Expand Up @@ -96,6 +99,7 @@ impl error::Error for Error {
Error::InterfaceNotFound => None,
Error::Address(_) => None,
Error::InputOutput(e) => Some(e),
Error::InputOutputAddress(e, _) => Some(e),
Error::ExcessData => None,
Error::Handshake(_) => None,
Error::IncorrectEndian => None,
Expand Down Expand Up @@ -125,6 +129,7 @@ impl fmt::Display for Error {
Error::Address(e) => write!(f, "address error: {e}"),
Error::ExcessData => write!(f, "excess data"),
Error::InputOutput(e) => write!(f, "I/O error: {e}"),
Error::InputOutputAddress(e, address) => write!(f, "I/O error: {e} for {address}"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be update too:

Suggested change
Error::InputOutputAddress(e, address) => write!(f, "I/O error: {e} for {address}"),
Error::InputOutputAddress(e, address) => write!(f, "failed to connect to `{address}`: {e}"),

Error::Handshake(e) => write!(f, "D-Bus handshake failed: {e}"),
Error::IncorrectEndian => write!(f, "incorrect endian"),
Error::InvalidField => write!(f, "invalid message field"),
Expand Down Expand Up @@ -160,6 +165,9 @@ impl Clone for Error {
Error::Address(e) => Error::Address(e.clone()),
Error::ExcessData => Error::ExcessData,
Error::InputOutput(e) => Error::InputOutput(e.clone()),
Error::InputOutputAddress(e, address) => {
Error::InputOutputAddress(e.clone(), address.clone())
}
Error::Handshake(e) => Error::Handshake(e.clone()),
Error::IncorrectEndian => Error::IncorrectEndian,
Error::InvalidField => Error::InvalidField,
Expand Down
Loading