Skip to content

Commit 2dbe08f

Browse files
committed
Added errors for desktop plugins
1 parent 6605ac5 commit 2dbe08f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/error.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,60 @@ use serde::{ser::Serializer, Serialize};
22

33
pub type Result<T> = std::result::Result<T, Error>;
44

5+
/// Replica of the tauri::plugin::mobile::ErrorResponse for desktop platforms.
6+
#[cfg(desktop)]
7+
#[derive(Debug, thiserror::Error, Clone, serde::Deserialize)]
8+
pub struct ErrorResponse<T = ()> {
9+
/// Error code.
10+
pub code: Option<String>,
11+
/// Error message.
12+
pub message: Option<String>,
13+
/// Optional error data.
14+
#[serde(flatten)]
15+
pub data: T,
16+
}
17+
18+
#[cfg(desktop)]
19+
impl<T> std::fmt::Display for ErrorResponse<T> {
20+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
if let Some(code) = &self.code {
22+
write!(f, "[{code}]")?;
23+
if self.message.is_some() {
24+
write!(f, " - ")?;
25+
}
26+
}
27+
if let Some(message) = &self.message {
28+
write!(f, "{message}")?;
29+
}
30+
Ok(())
31+
}
32+
}
33+
34+
/// Replica of the tauri::plugin::mobile::PluginInvokeError for desktop platforms.
35+
#[cfg(desktop)]
36+
#[derive(Debug, thiserror::Error)]
37+
pub enum PluginInvokeError {
38+
/// Error returned from direct desktop plugin.
39+
#[error(transparent)]
40+
InvokeRejected(#[from] ErrorResponse),
41+
/// Failed to deserialize response.
42+
#[error("failed to deserialize response: {0}")]
43+
CannotDeserializeResponse(serde_json::Error),
44+
/// Failed to serialize request payload.
45+
#[error("failed to serialize payload: {0}")]
46+
CannotSerializePayload(serde_json::Error),
47+
}
48+
549
#[derive(Debug, thiserror::Error)]
650
pub enum Error {
751
#[error(transparent)]
852
Io(#[from] std::io::Error),
953
#[cfg(mobile)]
1054
#[error(transparent)]
1155
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
56+
#[cfg(desktop)]
57+
#[error(transparent)]
58+
PluginInvoke(#[from] crate::error::PluginInvokeError),
1259
}
1360

1461
impl Serialize for Error {

0 commit comments

Comments
 (0)