ashpd/
error.rs

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