1use zbus::DBusError;
2
3use crate::desktop::{dynamic_launcher::UnexpectedIconError, request::ResponseError};
4
5#[allow(missing_docs)]
9#[derive(DBusError, Debug)]
10#[zbus(prefix = "org.freedesktop.portal.Error")]
11pub enum PortalError {
12 #[zbus(error)]
13 ZBus(zbus::Error),
15 Failed(String),
17 InvalidArgument(String),
19 NotFound(String),
21 Exist(String),
23 NotAllowed(String),
25 Cancelled(String),
27 WindowDestroyed(String),
29}
30
31#[derive(Debug)]
32#[non_exhaustive]
33pub enum Error {
35 Response(ResponseError),
37 Portal(PortalError),
39 Zbus(zbus::Error),
41 NoResponse,
43 ParseError(&'static str),
45 IO(std::io::Error),
47 #[cfg(feature = "pipewire")]
49 Pipewire(pipewire::Error),
50 InvalidAppID,
54 NulTerminated(usize),
56 RequiresVersion(u32, u32),
61 PortalNotFound(zbus::names::OwnedInterfaceName),
64 UnexpectedIcon,
67 #[cfg(feature = "backend")]
68 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}