Debian Codegraph @

src/error.rs

raw
1 use zbus::DBusError;
2
3 #[cfg(feature = "dynamic_launcher")]
4 use crate::desktop::dynamic_launcher::UnexpectedIconError;
5 use crate::desktop::request::ResponseError;
6
7 /// An error type that describes the various DBus errors.
8 ///
9 /// See <https://github.com/flatpak/xdg-desktop-portal/blob/1.20.0/src/xdp-utils.h#L88-L96>.
10 #[allow(missing_docs)]
11 #[derive(DBusError, Debug)]
12 #[zbus(prefix = "org.freedesktop.portal.Error")]
13 pub enum PortalError {
14 #[zbus(error)]
15 /// ZBus specific error.
16 ZBus(zbus::Error),
17 /// Request failed.
18 Failed(String),
19 /// Invalid arguments passed.
20 InvalidArgument(String),
21 /// Not found.
22 NotFound(String),
23 /// Exists already.
24 Exist(String),
25 /// Method not allowed to be called.
26 NotAllowed(String),
27 /// Request cancelled.
28 Cancelled(String),
29 /// Window destroyed.
30 WindowDestroyed(String),
31 }
32
33 #[derive(Debug)]
34 #[non_exhaustive]
35 /// The error type for ashpd.
36 pub enum Error {
37 /// The portal request didn't succeed.
38 Response(ResponseError),
39 /// Something Failed on the portal request.
40 Portal(PortalError),
41 /// A zbus::fdo specific error.
42 Zbus(zbus::Error),
43 /// A signal returned no response.
44 NoResponse,
45 /// Failed to parse a string into an enum variant
46 ParseError(&'static str),
47 /// Input/Output
48 IO(std::io::Error),
49 /// A pipewire error
50 #[cfg(feature = "pipewire")]
51 Pipewire(pipewire::Error),
52 /// Invalid AppId
53 ///
54 /// See <https://developer.gnome.org/documentation/tutorials/application-id.html#rules-for-application-ids>
55 InvalidAppID,
56 /// An error indicating that an interior nul byte was found
57 NulTerminated(usize),
58 /// Requires a newer interface version.
59 ///
60 /// The inner fields are the required version and the version advertised by
61 /// the interface.
62 RequiresVersion(u32, u32),
63 /// Returned when the portal wasn't found. Either the user has no portals
64 /// frontend installed or the frontend doesn't support the used portal.
65 PortalNotFound(zbus::names::OwnedInterfaceName),
66 /// An error indicating that a Icon::Bytes was expected but wrong type was
67 /// passed
68 UnexpectedIcon,
69 /// Failed to parse a URI.
70 Uri(crate::uri::ParseError),
71 }
72
73 impl std::error::Error for Error {}
74
75 impl std::fmt::Display for Error {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 match self {
78 Self::Response(e) => match e {
79 ResponseError::Cancelled => write!(f, "Portal request was cancelled"),
80 ResponseError::Other => {
81 write!(f, "Portal request didn't succeed with no information")
82 }
83 },
84 Self::Zbus(e) => write!(f, "ZBus Error: {e}"),
85 Self::Portal(e) => write!(f, "Portal request failed: {e}"),
86 Self::NoResponse => f.write_str("Portal request didn't receive a response"),
87 Self::IO(e) => write!(f, "IO: {e}"),
88 #[cfg(feature = "pipewire")]
89 Self::Pipewire(e) => write!(f, "Pipewire: {e}"),
90 Self::ParseError(e) => f.write_str(e),
91 Self::InvalidAppID => f.write_str("Invalid app id"),
92 Self::NulTerminated(u) => write!(f, "Nul byte found in provided data at position {u}"),
93 Self::RequiresVersion(required, current) => write!(
94 f,
95 "This interface requires version {required}, but {current} is available"
96 ),
97 Self::PortalNotFound(portal) => {
98 write!(f, "A portal frontend implementing `{portal}` was not found")
99 }
100 Self::UnexpectedIcon => write!(
101 f,
102 "Expected icon of type Icon::Bytes but a different type was used."
103 ),
104 Self::Uri(e) => write!(f, "URI parse error: {e}"),
105 }
106 }
107 }
108
109 impl From<ResponseError> for Error {
110 fn from(e: ResponseError) -> Self {
111 Self::Response(e)
112 }
113 }
114
115 impl From<PortalError> for Error {
116 fn from(e: PortalError) -> Self {
117 Self::Portal(e)
118 }
119 }
120
121 #[cfg(feature = "pipewire")]
122 impl From<pipewire::Error> for Error {
123 fn from(e: pipewire::Error) -> Self {
124 Self::Pipewire(e)
125 }
126 }
127
128 impl From<zbus::fdo::Error> for Error {
129 fn from(e: zbus::fdo::Error) -> Self {
130 Self::Zbus(zbus::Error::FDO(Box::new(e)))
131 }
132 }
133
134 impl From<zbus::Error> for Error {
135 fn from(e: zbus::Error) -> Self {
136 Self::Zbus(e)
137 }
138 }
139
140 impl From<zbus::zvariant::Error> for Error {
141 fn from(e: zbus::zvariant::Error) -> Self {
142 Self::Zbus(zbus::Error::Variant(e))
143 }
144 }
145
146 impl From<std::io::Error> for Error {
147 fn from(e: std::io::Error) -> Self {
148 Self::IO(e)
149 }
150 }
151
152 #[cfg(feature = "dynamic_launcher")]
153 impl From<UnexpectedIconError> for Error {
154 fn from(_: UnexpectedIconError) -> Self {
155 Self::UnexpectedIcon
156 }
157 }
158
159 impl From<crate::uri::ParseError> for Error {
160 fn from(e: crate::uri::ParseError) -> Self {
161 Self::Uri(e)
162 }
163 }