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#[derive(Debug)]
14#[non_exhaustive]
15#[allow(clippy::upper_case_acronyms)]
16pub enum Error {
17 InterfaceNotFound,
19 Address(String),
21 InputOutput(Arc<io::Error>),
23 InvalidField,
25 ExcessData,
27 Variant(VariantError),
29 Names(NamesError),
31 IncorrectEndian,
33 Handshake(String),
35 InvalidReply,
37 MethodError(OwnedErrorName, Option<String>, Message),
41 MissingField,
43 InvalidGUID,
45 Unsupported,
47 FDO(Box<fdo::Error>),
49 NameTaken,
51 InvalidMatchRule,
55 Failure(String),
57 MissingParameter(&'static str),
59 InvalidSerial,
61 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
235impl From<Message> for Error {
237 fn from(message: Message) -> Error {
238 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
257pub type Result<T> = std::result::Result<T, Error>;