Skip to main content

robius_share/
error.rs

1pub type Result<T> = std::result::Result<T, Error>;
2
3/// Errors encountered when showing a native share sheet.
4#[derive(Debug)]
5pub enum Error {
6    /// Couldn't acquire the Android environment.
7    ///
8    /// See the `robius-android-env` crate for more details.
9    AndroidEnvironment,
10    #[cfg(target_os = "android")]
11    Java(jni::errors::Error),
12    #[cfg(target_os = "windows")]
13    Windows(windows::core::Error),
14    /// The share sheet wasn't started from the main UI thread, as is required.
15    NotMainThread,
16    /// A file attachment or other filesystem operation failed.
17    Io(std::io::Error),
18    /// The share sheet has no text, URL, or attachment to share.
19    Empty,
20    /// A URL, URI, file path, or MIME type was malformed or otherwise invalid.
21    InvalidItem,
22    /// No app or system service is available to receive this share.
23    NoHandler,
24    /// The native UI is already showing a share sheet.
25    /// The user must dismiss that one and try again.
26    AlreadyOpen,
27    /// The platform supports sharing, but not the given share item.
28    UnsupportedItem,
29    /// This platform is unsupported.
30    Unsupported,
31    /// An unknown error occurred.
32    Unknown,
33}
34
35#[cfg(target_os = "android")]
36impl From<jni::errors::Error> for Error {
37    fn from(value: jni::errors::Error) -> Self {
38        Self::Java(value)
39    }
40}
41
42#[cfg(target_os = "windows")]
43impl From<windows::core::Error> for Error {
44    fn from(value: windows::core::Error) -> Self {
45        Self::Windows(value)
46    }
47}
48
49impl From<std::io::Error> for Error {
50    fn from(value: std::io::Error) -> Self {
51        Self::Io(value)
52    }
53}
54
55impl std::fmt::Display for Error {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self {
58            Error::AndroidEnvironment => f.write_str("couldn't access the Android/Java environment"),
59            #[cfg(target_os = "android")]
60            Error::Java(err) => write!(f, "Java error: {err}"),
61            #[cfg(target_os = "windows")]
62            Error::Windows(err) => write!(f, "Windows API error: {err}"),
63            Error::NotMainThread => f.write_str("must be called from the main UI thread"),
64            Error::Io(err) => write!(f, "I/O error: {err}"),
65            Error::Empty => f.write_str("nothing to share"),
66            Error::InvalidItem => f.write_str("invalid share item"),
67            Error::NoHandler => f.write_str("no app is available to receive this share"),
68            Error::AlreadyOpen => f.write_str("another share sheet is already open"),
69            Error::UnsupportedItem => f.write_str("this share item is unsupported on the current platform"),
70            Error::Unsupported => f.write_str("this platform is unsupported"),
71            Error::Unknown => f.write_str("unknown error"),
72        }
73    }
74}
75
76impl std::error::Error for Error {
77    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
78        match self {
79            Error::Io(err) => Some(err),
80            #[cfg(target_os = "android")]
81            Error::Java(err) => Some(err),
82            #[cfg(target_os = "windows")]
83            Error::Windows(err) => Some(err),
84            _ => None,
85        }
86    }
87}