yew_oauth2/agent/
error.rs

1use crate::context::OAuth2Context;
2use core::fmt::{Display, Formatter};
3
4/// An error with the OAuth2 agent
5#[derive(Debug)]
6pub enum OAuth2Error {
7    /// Not initialized
8    NotInitialized,
9    /// Configuration error
10    Configuration(String),
11    /// Failed to start login
12    StartLogin(String),
13    /// Failed to handle login result
14    LoginResult(String),
15    /// Failed to handle token refresh
16    Refresh(String),
17    /// Failing storing information
18    Storage(String),
19    /// Internal error
20    Internal(String),
21}
22
23impl Display for OAuth2Error {
24    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::NotInitialized => f.write_str("not initialized"),
27            Self::Configuration(err) => write!(f, "configuration error: {err}"),
28            Self::StartLogin(err) => write!(f, "start login error: {err}"),
29            Self::LoginResult(err) => write!(f, "login result: {err}"),
30            Self::Refresh(err) => write!(f, "refresh error: {err}"),
31            Self::Storage(err) => write!(f, "storage error: {err}"),
32            Self::Internal(err) => write!(f, "internal error: {err}"),
33        }
34    }
35}
36
37impl std::error::Error for OAuth2Error {}
38
39impl From<OAuth2Error> for OAuth2Context {
40    fn from(err: OAuth2Error) -> Self {
41        OAuth2Context::Failed(err.to_string())
42    }
43}
44
45impl OAuth2Error {
46    pub(crate) fn storage_key_empty(key: impl Display) -> Self {
47        Self::Storage(format!("Missing value for key: {key}"))
48    }
49}