Skip to main content

stakpak_shared/oauth/
error.rs

1//! OAuth error types
2
3use thiserror::Error;
4
5/// Errors that can occur during OAuth operations
6#[derive(Error, Debug)]
7pub enum OAuthError {
8    /// PKCE challenge was not initialized before token exchange
9    #[error("PKCE challenge not initialized. Call generate_auth_url() first.")]
10    PkceNotInitialized,
11
12    /// Token exchange failed
13    #[error("Token exchange failed: {0}")]
14    TokenExchangeFailed(String),
15
16    /// Token refresh failed
17    #[error("Token refresh failed: {0}")]
18    TokenRefreshFailed(String),
19
20    /// API key creation failed
21    #[error("Failed to create API key from OAuth tokens")]
22    ApiKeyCreationFailed,
23
24    /// Unknown authentication method
25    #[error("Unknown authentication method: {0}")]
26    UnknownMethod(String),
27
28    /// Invalid header value
29    #[error("Invalid header value")]
30    InvalidHeader,
31
32    /// HTTP request error
33    #[error("HTTP error: {0}")]
34    HttpError(#[from] reqwest::Error),
35
36    /// JSON parsing error
37    #[error("JSON parse error: {0}")]
38    JsonError(#[from] serde_json::Error),
39
40    /// Parse error
41    #[error("Parse error: {0}")]
42    ParseError(String),
43
44    /// Invalid authorization code format
45    #[error("Invalid authorization code format: {0}")]
46    InvalidCodeFormat(String),
47
48    /// Provider not found
49    #[error("Provider not found: {0}")]
50    ProviderNotFound(String),
51
52    /// OAuth not supported for this method
53    #[error("OAuth not supported for this authentication method")]
54    OAuthNotSupported,
55
56    /// File I/O error
57    #[error("File I/O error: {0}")]
58    IoError(#[from] std::io::Error),
59
60    /// TOML serialization error
61    #[error("TOML serialization error: {0}")]
62    TomlSerError(#[from] toml::ser::Error),
63
64    /// TOML deserialization error
65    #[error("TOML deserialization error: {0}")]
66    TomlDeError(#[from] toml::de::Error),
67
68    /// Authentication required
69    #[error("Authentication required. Run 'stakpak auth login' to authenticate.")]
70    AuthRequired,
71
72    /// Token expired and refresh failed
73    #[error("Token expired. Please re-authenticate with 'stakpak auth login'.")]
74    TokenExpired,
75}
76
77impl OAuthError {
78    /// Create a token exchange failed error
79    pub fn token_exchange_failed(msg: impl Into<String>) -> Self {
80        Self::TokenExchangeFailed(msg.into())
81    }
82
83    /// Create a token refresh failed error
84    pub fn token_refresh_failed(msg: impl Into<String>) -> Self {
85        Self::TokenRefreshFailed(msg.into())
86    }
87
88    /// Create a parse error
89    pub fn parse_error(msg: impl Into<String>) -> Self {
90        Self::ParseError(msg.into())
91    }
92
93    /// Create an invalid code format error
94    pub fn invalid_code_format(msg: impl Into<String>) -> Self {
95        Self::InvalidCodeFormat(msg.into())
96    }
97
98    /// Create a provider not found error
99    pub fn provider_not_found(provider: impl Into<String>) -> Self {
100        Self::ProviderNotFound(provider.into())
101    }
102
103    /// Create an unknown method error
104    pub fn unknown_method(method: impl Into<String>) -> Self {
105        Self::UnknownMethod(method.into())
106    }
107}
108
109/// Result type alias for OAuth operations
110pub type OAuthResult<T> = Result<T, OAuthError>;