dbus2 / zbus-old

Rust D-Bus crate.
https://gitlab.freedesktop.org/dbus/zbus
Other
49 stars 13 forks source link

Find solution for DBusError that works for clients and interfaces #204

Closed zeenix closed 1 year ago

zeenix commented 3 years ago

In GitLab by @rosslannen on Aug 25, 2021, 22:46

The following discussion from !368 should be addressed:

zeenix commented 2 years ago

@rosslannen Hi, quite a bit has changed wrt to this in main branch and also we don't panic on interface methods returning zbus::Error anymore. Could you kindly check if this issue is still relevant?

zeenix commented 2 years ago

In GitLab by @rosslannen on Sep 28, 2021, 18:09

@zeenix I like the direction that the changes are headed. It is a definite improvement over when this issue was first raised.

However, there are still a couple of strange things I see still while looking at it. The new DBusError trait is great, but it only has the one method - however, the derive macro still looks like it generates methods on the struct that are not part of the trait (name(), description(), and reply()). I would see this as pretty unexpected behavior, especially when all derives in std - from what I can remember - just derive the trait, and nothing else. This is also pretty consistent with other derive macros, such as serde or thiserror. I think those methods should either be moved into the DBusError trait if they are important to it, or removed from the DBusError derive. It looks like there are some other traits that are automatically derived that I am not sure should be - at least not automatically, with no other input. In general, I prefer something like the approach that thiserror takes: just the derive generates just the trait, but then other stuff can be added using the #[error(...)] attribute or #[from] attribute within the enum to generate extra stuff.

The current solution also doesn't solve one of my original issues with the trait, as it looks like it still requires a ZBus(zbus::Error) variant on the enum in order to be used, even if it is only used on the server side.

Something that could work for this would be to have a #[dbus_error(...)] attribute that goes along with the derive that could be added to control things like generating a Display impl or creating the From<zbus::Error> impl (or maybe even other From<OtherError> impls). And then to use the error in client use cases, it would just have a trait bound of DBusError + From<zbus::Error> and the server side would just require DBusError. That way it doesn't force the user to create that ZBus variant on the enum for server use only.

I don't have a lot of experience writing macros, but I could take a try at creating an implementation this week. Stuff at work has lightened up a bit, and I have some time available.

zeenix commented 2 years ago

@rosslannen thanks for explaining!

the new DBusError trait is great, but it only has the one method - however, the derive macro still looks like it generates methods on the struct that are not part of the trait (name(), description(), and reply()).

Oh, somehow I missed that part. It should have been done, yes. However, this part would be easiest to fix.

It looks like there are some other traits that are automatically derived that I am not sure should be - at least not automatically, with no other input.

I think the idea here was that you'd use this macro to easily create an error type to use on D-Bus, rather than making your error type work with D-Bus. However, I see the value in the latter.

Something that could work for this would be to have a #[dbus_error(...)] attribute that goes along with the derive that could be added to control things like generating a Display impl or creating the From<zbus::Error> impl (or maybe even other From<OtherError> impls). And then to use the error in client use cases, it would just have a trait bound of DBusError + From<zbus::Error> and the server side would just require DBusError. That way it doesn't force the user to create that ZBus variant on the enum for server use only.

Hm.. yeah that could work. However, since you say that you've a bit of a learn curve on writing macros, I'd suggest splitting this off (into a separate issue/MR) and first the get easier bits done. :)

Stuff at work has lightened up a bit, and I have some time available.

Awesome! :)

zeenix commented 2 years ago

mentioned in commit d842f8f23c02434bfba73800da0418c65fc56e07