zbus/
error.rs

1use std::{convert::Infallible, error, fmt, io, sync::Arc};
2use zbus_names::{Error as NamesError, InterfaceName, OwnedErrorName};
3use zvariant::{Error as VariantError, ObjectPath};
4
5use crate::{
6    fdo,
7    message::{Message, Type},
8};
9
10/// The error type for `zbus`.
11///
12/// The various errors that can be reported by this crate.
13#[derive(Debug)]
14#[non_exhaustive]
15#[allow(clippy::upper_case_acronyms)]
16pub enum Error {
17    /// Interface not found.
18    InterfaceNotFound,
19    /// Invalid D-Bus address.
20    Address(String),
21    /// An I/O error.
22    InputOutput(Arc<io::Error>),
23    /// Invalid message field.
24    InvalidField,
25    /// Data too large.
26    ExcessData,
27    /// A [zvariant](https://docs.rs/zvariant) error.
28    Variant(VariantError),
29    /// A [zbus_names](https://docs.rs/zbus_names) error.
30    Names(NamesError),
31    /// Endian signature invalid or doesn't match expectation.
32    IncorrectEndian,
33    /// Initial handshake error.
34    Handshake(String),
35    /// Unexpected or incorrect reply.
36    InvalidReply,
37    /// A D-Bus method error reply.
38    // According to the spec, there can be all kinds of details in D-Bus errors but nobody adds
39    // anything more than a string description.
40    MethodError(OwnedErrorName, Option<String>, Message),
41    /// A required field is missing in the message headers.
42    MissingField,
43    /// Invalid D-Bus GUID.
44    InvalidGUID,
45    /// Unsupported function, or support currently lacking.
46    Unsupported,
47    /// A [`fdo::Error`] transformed into [`Error`].
48    FDO(Box<fdo::Error>),
49    /// The requested name was already claimed by another peer.
50    NameTaken,
51    /// Invalid [match rule][MR] string.
52    ///
53    /// [MR]: https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
54    InvalidMatchRule,
55    /// Generic error.
56    Failure(String),
57    /// A required parameter was missing.
58    MissingParameter(&'static str),
59    /// Serial number in the message header is 0 (which is invalid).
60    InvalidSerial,
61    /// The given interface already exists at the given path.
62    InterfaceExists(InterfaceName<'static>, ObjectPath<'static>),
63}
64
65impl PartialEq for Error {
66    fn eq(&self, other: &Self) -> bool {
67        match (self, other) {
68            (Self::Address(_), Self::Address(_)) => true,
69            (Self::InterfaceNotFound, Self::InterfaceNotFound) => true,
70            (Self::Handshake(_), Self::Handshake(_)) => true,
71            (Self::InvalidReply, Self::InvalidReply) => true,
72            (Self::ExcessData, Self::ExcessData) => true,
73            (Self::IncorrectEndian, Self::IncorrectEndian) => true,
74            (Self::MethodError(_, _, _), Self::MethodError(_, _, _)) => true,
75            (Self::MissingField, Self::MissingField) => true,
76            (Self::InvalidGUID, Self::InvalidGUID) => true,
77            (Self::InvalidSerial, Self::InvalidSerial) => true,
78            (Self::Unsupported, Self::Unsupported) => true,
79            (Self::FDO(s), Self::FDO(o)) => s == o,
80            (Self::InvalidField, Self::InvalidField) => true,
81            (Self::InvalidMatchRule, Self::InvalidMatchRule) => true,
82            (Self::Variant(s), Self::Variant(o)) => s == o,
83            (Self::Names(s), Self::Names(o)) => s == o,
84            (Self::NameTaken, Self::NameTaken) => true,
85            (Error::InputOutput(_), Self::InputOutput(_)) => false,
86            (Self::Failure(s1), Self::Failure(s2)) => s1 == s2,
87            (Self::InterfaceExists(s1, s2), Self::InterfaceExists(o1, o2)) => s1 == o1 && s2 == o2,
88            (_, _) => false,
89        }
90    }
91}
92
93impl error::Error for Error {
94    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
95        match self {
96            Error::InterfaceNotFound => None,
97            Error::Address(_) => None,
98            Error::InputOutput(e) => Some(e),
99            Error::ExcessData => None,
100            Error::Handshake(_) => None,
101            Error::IncorrectEndian => None,
102            Error::Variant(e) => Some(e),
103            Error::Names(e) => Some(e),
104            Error::InvalidReply => None,
105            Error::MethodError(_, _, _) => None,
106            Error::InvalidGUID => None,
107            Error::Unsupported => None,
108            Error::FDO(e) => Some(e),
109            Error::InvalidField => None,
110            Error::MissingField => None,
111            Error::NameTaken => None,
112            Error::InvalidMatchRule => None,
113            Error::Failure(_) => None,
114            Error::MissingParameter(_) => None,
115            Error::InvalidSerial => None,
116            Error::InterfaceExists(_, _) => None,
117        }
118    }
119}
120
121impl fmt::Display for Error {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        match self {
124            Error::InterfaceNotFound => write!(f, "Interface not found"),
125            Error::Address(e) => write!(f, "address error: {e}"),
126            Error::ExcessData => write!(f, "excess data"),
127            Error::InputOutput(e) => write!(f, "I/O error: {e}"),
128            Error::Handshake(e) => write!(f, "D-Bus handshake failed: {e}"),
129            Error::IncorrectEndian => write!(f, "incorrect endian"),
130            Error::InvalidField => write!(f, "invalid message field"),
131            Error::Variant(e) => write!(f, "{e}"),
132            Error::Names(e) => write!(f, "{e}"),
133            Error::InvalidReply => write!(f, "Invalid D-Bus method reply"),
134            Error::MissingField => write!(f, "A required field is missing from message headers"),
135            Error::MethodError(name, detail, _reply) => write!(
136                f,
137                "{}: {}",
138                **name,
139                detail.as_ref().map(|s| s.as_str()).unwrap_or("no details")
140            ),
141            Error::InvalidGUID => write!(f, "Invalid GUID"),
142            Error::Unsupported => write!(f, "Connection support is lacking"),
143            Error::FDO(e) => write!(f, "{e}"),
144            Error::NameTaken => write!(f, "name already taken on the bus"),
145            Error::InvalidMatchRule => write!(f, "Invalid match rule string"),
146            Error::Failure(e) => write!(f, "{e}"),
147            Error::MissingParameter(p) => {
148                write!(f, "Parameter `{p}` was not specified but it is required")
149            }
150            Error::InvalidSerial => write!(f, "Serial number in the message header is 0"),
151            Error::InterfaceExists(i, p) => write!(f, "Interface `{i}` already exists at `{p}`"),
152        }
153    }
154}
155
156impl Clone for Error {
157    fn clone(&self) -> Self {
158        match self {
159            Error::InterfaceNotFound => Error::InterfaceNotFound,
160            Error::Address(e) => Error::Address(e.clone()),
161            Error::ExcessData => Error::ExcessData,
162            Error::InputOutput(e) => Error::InputOutput(e.clone()),
163            Error::Handshake(e) => Error::Handshake(e.clone()),
164            Error::IncorrectEndian => Error::IncorrectEndian,
165            Error::InvalidField => Error::InvalidField,
166            Error::Variant(e) => Error::Variant(e.clone()),
167            Error::Names(e) => Error::Names(e.clone()),
168            Error::InvalidReply => Error::InvalidReply,
169            Error::MissingField => Error::MissingField,
170            Error::MethodError(name, detail, reply) => {
171                Error::MethodError(name.clone(), detail.clone(), reply.clone())
172            }
173            Error::InvalidGUID => Error::InvalidGUID,
174            Error::Unsupported => Error::Unsupported,
175            Error::FDO(e) => Error::FDO(e.clone()),
176            Error::NameTaken => Error::NameTaken,
177            Error::InvalidMatchRule => Error::InvalidMatchRule,
178            Error::Failure(e) => Error::Failure(e.clone()),
179            Error::MissingParameter(p) => Error::MissingParameter(p),
180            Error::InvalidSerial => Error::InvalidSerial,
181            Error::InterfaceExists(i, p) => Error::InterfaceExists(i.clone(), p.clone()),
182        }
183    }
184}
185
186impl From<io::Error> for Error {
187    fn from(val: io::Error) -> Self {
188        Error::InputOutput(Arc::new(val))
189    }
190}
191
192#[cfg(unix)]
193impl From<nix::Error> for Error {
194    fn from(val: nix::Error) -> Self {
195        io::Error::from_raw_os_error(val as i32).into()
196    }
197}
198
199impl From<VariantError> for Error {
200    fn from(val: VariantError) -> Self {
201        Error::Variant(val)
202    }
203}
204
205impl From<zvariant::signature::Error> for Error {
206    fn from(e: zvariant::signature::Error) -> Self {
207        zvariant::Error::from(e).into()
208    }
209}
210
211impl From<NamesError> for Error {
212    fn from(val: NamesError) -> Self {
213        match val {
214            NamesError::Variant(e) => Error::Variant(e),
215            e => Error::Names(e),
216        }
217    }
218}
219
220impl From<fdo::Error> for Error {
221    fn from(val: fdo::Error) -> Self {
222        match val {
223            fdo::Error::ZBus(e) => e,
224            e => Error::FDO(Box::new(e)),
225        }
226    }
227}
228
229impl From<Infallible> for Error {
230    fn from(i: Infallible) -> Self {
231        match i {}
232    }
233}
234
235// For messages that are D-Bus error returns
236impl From<Message> for Error {
237    fn from(message: Message) -> Error {
238        // FIXME: Instead of checking this, we should have Method as trait and specific types for
239        // each message type.
240        let header = message.header();
241        if header.primary().msg_type() != Type::Error {
242            return Error::InvalidReply;
243        }
244
245        if let Some(name) = header.error_name() {
246            let name = name.to_owned().into();
247            match message.body().deserialize_unchecked::<&str>() {
248                Ok(detail) => Error::MethodError(name, Some(String::from(detail)), message),
249                Err(_) => Error::MethodError(name, None, message),
250            }
251        } else {
252            Error::InvalidReply
253        }
254    }
255}
256
257/// Alias for a `Result` with the error type `zbus::Error`.
258pub type Result<T> = std::result::Result<T, Error>;