Skip to main content

tauri_plugin_oauth_session/
error.rs

1use serde::{Serialize, Serializer};
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, thiserror::Error)]
6#[non_exhaustive]
7pub enum Error {
8    #[error("user canceled the authentication session")]
9    UserCanceled,
10    #[error("the platform could not present the authentication session")]
11    PresentationFailed,
12    #[error("an authentication session is already in progress")]
13    SessionBusy,
14    #[error("the authentication session failed: {0}")]
15    SessionFailed(String),
16    #[error("OAuth sessions are only supported on iOS and Android")]
17    UnsupportedPlatform,
18    #[cfg(mobile)]
19    #[error(transparent)]
20    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
21}
22
23/// Stable, machine-readable error code that the JS layer can switch on
24/// without parsing free-form messages.
25impl Error {
26    pub fn code(&self) -> &'static str {
27        match self {
28            Error::UserCanceled => "USER_CANCELED",
29            Error::PresentationFailed => "PRESENTATION_FAILED",
30            Error::SessionBusy => "SESSION_BUSY",
31            Error::SessionFailed(_) => "SESSION_FAILED",
32            Error::UnsupportedPlatform => "UNSUPPORTED_PLATFORM",
33            #[cfg(mobile)]
34            Error::PluginInvoke(_) => "PLUGIN_INVOKE_FAILED",
35        }
36    }
37}
38
39#[derive(Serialize)]
40struct SerializedError<'a> {
41    code: &'a str,
42    message: String,
43}
44
45impl Serialize for Error {
46    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
47    where
48        S: Serializer,
49    {
50        SerializedError {
51            code: self.code(),
52            message: self.to_string(),
53        }
54        .serialize(serializer)
55    }
56}