Skip to main content

cirrus_auth/
error.rs

1//! Error types for `cirrus-auth`.
2//!
3//! Salesforce OAuth token endpoints return errors in the standard
4//! `{error, error_description}` shape (RFC 6749 §5.2), which
5//! [`AuthError::OAuth`] models directly. Everything else — missing
6//! builder fields, transport failures, malformed responses — is
7//! categorized into the remaining variants.
8//!
9//! The companion `cirrus` crate carries a `From<AuthError> for
10//! CirrusError` impl so REST call sites that invoke an
11//! [`crate::AuthSession`] can use `?` and surface the auth failure as
12//! part of the unified client error.
13//!
14//! `AuthError` is intentionally `non_exhaustive` so we can grow it
15//! without a SemVer break.
16
17use thiserror::Error;
18
19/// Specialized `Result` type for `cirrus-auth` operations.
20pub type AuthResult<T> = Result<T, AuthError>;
21
22/// Errors produced while acquiring or refreshing a Salesforce OAuth
23/// session.
24#[derive(Debug, Error)]
25#[non_exhaustive]
26pub enum AuthError {
27    /// A required builder field was not set.
28    #[error("missing required builder field: {0}")]
29    MissingField(&'static str),
30
31    /// OAuth token endpoint returned an error response (`error` /
32    /// `error_description` shape from RFC 6749 §5.2).
33    #[error("OAuth error: {error}{}", .error_description.as_deref().map(|d| format!(" — {d}")).unwrap_or_default())]
34    OAuth {
35        error: String,
36        error_description: Option<String>,
37    },
38
39    /// Catch-all for auth failures not modelled by a dedicated variant
40    /// (system clock skew, JWT signing failure, CSPRNG failure,
41    /// malformed token responses, etc.). Carries the underlying
42    /// message.
43    #[error("authentication failed: {0}")]
44    Other(String),
45
46    /// Network or transport-level HTTP failure while contacting an
47    /// OAuth endpoint.
48    #[error("HTTP request failed: {0}")]
49    Http(#[from] reqwest::Error),
50
51    /// JSON serialization or deserialization failure.
52    #[error("serialization error: {0}")]
53    Serialization(#[from] serde_json::Error),
54
55    /// URL parsing failure (instance URL, redirect URI, login URL,
56    /// etc.).
57    #[error("invalid URL: {0}")]
58    Url(#[from] url::ParseError),
59}