Skip to main content

mailledger_oauth/
error.rs

1//! Error types for `OAuth2` operations.
2
3use std::io;
4
5/// Result type alias for `OAuth2` operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// `OAuth2` error types.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// I/O error.
12    #[error("I/O error: {0}")]
13    Io(#[from] io::Error),
14
15    /// HTTP request error.
16    #[error("HTTP error: {0}")]
17    Http(#[from] reqwest::Error),
18
19    /// JSON parsing error.
20    #[error("JSON error: {0}")]
21    Json(#[from] serde_json::Error),
22
23    /// `OAuth2` error from server.
24    #[error("OAuth2 error: {error} - {description}")]
25    OAuth {
26        /// Error code (e.g., `invalid_grant`).
27        error: String,
28        /// Human-readable description.
29        description: String,
30    },
31
32    /// Token expired.
33    #[error("Token expired")]
34    TokenExpired,
35
36    /// No refresh token available.
37    #[error("No refresh token available")]
38    NoRefreshToken,
39
40    /// Invalid token response.
41    #[error("Invalid token response: {0}")]
42    InvalidResponse(String),
43
44    /// Authorization timeout.
45    #[error("Authorization timed out after {0} seconds")]
46    Timeout(u64),
47
48    /// User denied authorization.
49    #[error("User denied authorization")]
50    AccessDenied,
51
52    /// Invalid configuration.
53    #[error("Invalid configuration: {0}")]
54    InvalidConfig(String),
55
56    /// URL parsing error.
57    #[error("URL error: {0}")]
58    UrlError(#[from] url::ParseError),
59}
60
61impl Error {
62    /// Creates an OAuth error from error code and description.
63    #[must_use]
64    pub fn oauth_error(error: impl Into<String>, description: impl Into<String>) -> Self {
65        Self::OAuth {
66            error: error.into(),
67            description: description.into(),
68        }
69    }
70}