atproto_oauth_axum/
errors.rs

1//! # Structured Error Types for OAuth Axum Handlers
2//!
3//! Comprehensive error handling for AT Protocol OAuth Axum web handlers using structured error types
4//! with the `thiserror` library. All errors follow the project convention of prefixed error codes
5//! with descriptive messages.
6//!
7//! ## Error Categories
8//!
9//! - **`OAuthCallbackError`** (callback-1 to callback-7): OAuth callback handler errors
10//! - **`OAuthLoginError`** (login-1 to login-11): OAuth login CLI tool errors
11//!
12//! ## Error Format
13//!
14//! All errors use the standardized format: `error-atproto-oauth-axum-{domain}-{number} {message}: {details}`
15
16use thiserror::Error;
17
18/// Error types that can occur during OAuth callback handling.
19///
20/// These errors represent failures in the OAuth authorization callback flow
21/// including request validation and token exchange operations.
22#[derive(Debug, Error)]
23pub enum OAuthCallbackError {
24    /// Occurs when no OAuth request is found for the provided state parameter
25    #[error("error-atproto-oauth-axum-callback-1 No OAuth request found for state")]
26    NoOAuthRequestFound,
27
28    /// Occurs when the issuer in the callback doesn't match the stored OAuth request
29    #[error(
30        "error-atproto-oauth-axum-callback-2 Invalid issuer: expected {expected}, got {actual}"
31    )]
32    InvalidIssuer {
33        /// The expected issuer from the stored OAuth request
34        expected: String,
35        /// The actual issuer from the callback
36        actual: String,
37    },
38
39    /// Occurs when no DID document is found for the OAuth request
40    #[error("error-atproto-oauth-axum-callback-3 No DID document found for OAuth request")]
41    NoDIDDocumentFound,
42
43    /// Occurs when no signing key is found for the OAuth request
44    #[error("error-atproto-oauth-axum-callback-4 No signing key found for OAuth request")]
45    NoSigningKeyFound,
46
47    /// Occurs when an underlying operation fails with an anyhow error
48    #[error("error-atproto-oauth-axum-callback-5 Operation failed: {error}")]
49    OperationFailed {
50        /// The underlying anyhow error
51        error: anyhow::Error,
52    },
53
54    /// Occurs when key operations fail
55    #[error("error-atproto-oauth-axum-callback-6 Key operation failed: {error}")]
56    KeyOperationFailed {
57        /// The underlying key error
58        error: atproto_identity::errors::KeyError,
59    },
60
61    /// Occurs when OAuth client operations fail
62    #[error("error-atproto-oauth-axum-callback-7 OAuth client operation failed: {error}")]
63    OAuthClientOperationFailed {
64        /// The underlying OAuth client error
65        error: atproto_oauth::errors::OAuthClientError,
66    },
67}
68
69impl From<anyhow::Error> for OAuthCallbackError {
70    fn from(error: anyhow::Error) -> Self {
71        OAuthCallbackError::OperationFailed { error }
72    }
73}
74
75impl From<atproto_identity::errors::KeyError> for OAuthCallbackError {
76    fn from(error: atproto_identity::errors::KeyError) -> Self {
77        OAuthCallbackError::KeyOperationFailed { error }
78    }
79}
80
81impl From<atproto_oauth::errors::OAuthClientError> for OAuthCallbackError {
82    fn from(error: atproto_oauth::errors::OAuthClientError) -> Self {
83        OAuthCallbackError::OAuthClientOperationFailed { error }
84    }
85}
86
87/// Error types that can occur during OAuth login CLI operations.
88///
89/// These errors represent failures in the OAuth login command-line tool
90/// including subject resolution, DID operations, and OAuth flow initiation.
91#[derive(Debug, Error)]
92pub enum OAuthLoginError {
93    /// Occurs when subject resolution fails
94    #[error("error-atproto-oauth-axum-login-1 Failed to resolve subject: {error}")]
95    SubjectResolutionFailed {
96        /// The underlying resolution error
97        error: anyhow::Error,
98    },
99
100    /// Occurs when PLC directory query fails
101    #[error("error-atproto-oauth-axum-login-2 Failed to query PLC directory: {error}")]
102    PLCQueryFailed {
103        /// The underlying PLC error
104        error: anyhow::Error,
105    },
106
107    /// Occurs when web DID query fails
108    #[error("error-atproto-oauth-axum-login-3 Failed to query web DID: {error}")]
109    WebDIDQueryFailed {
110        /// The underlying web DID error
111        error: anyhow::Error,
112    },
113
114    /// Occurs when an unsupported DID method is encountered
115    #[error("error-atproto-oauth-axum-login-4 Unsupported DID method: {did}")]
116    UnsupportedDIDMethod {
117        /// The unsupported DID identifier
118        did: String,
119    },
120
121    /// Occurs when no PDS endpoint is found in the DID document
122    #[error("error-atproto-oauth-axum-login-5 No PDS endpoint found in DID document")]
123    NoPDSEndpointFound,
124
125    /// Occurs when PDS resources retrieval fails
126    #[error("error-atproto-oauth-axum-login-6 Failed to get PDS resources: {error}")]
127    PDSResourcesFailed {
128        /// The underlying PDS resources error
129        error: anyhow::Error,
130    },
131
132    /// Occurs when DPoP key generation fails
133    #[error("error-atproto-oauth-axum-login-7 Failed to generate DPoP key: {error}")]
134    DPoPKeyGenerationFailed {
135        /// The underlying key generation error
136        error: anyhow::Error,
137    },
138
139    /// Occurs when private signing key parsing fails
140    #[error("error-atproto-oauth-axum-login-8 Invalid private signing key: {error}")]
141    InvalidPrivateSigningKey {
142        /// The underlying key parsing error
143        error: anyhow::Error,
144    },
145
146    /// Occurs when OAuth initialization fails
147    #[error("error-atproto-oauth-axum-login-9 OAuth init failed: {error}")]
148    OAuthInitFailed {
149        /// The underlying OAuth initialization error
150        error: anyhow::Error,
151    },
152
153    /// Occurs when public key derivation fails
154    #[error("error-atproto-oauth-axum-login-10 Failed to derive public key: {error}")]
155    PublicKeyDerivationFailed {
156        /// The underlying key derivation error
157        error: anyhow::Error,
158    },
159
160    /// Occurs when OAuth request storage fails
161    #[error("error-atproto-oauth-axum-login-11 Failed to store OAuth request: {error}")]
162    OAuthRequestStorageFailed {
163        /// The underlying storage error
164        error: anyhow::Error,
165    },
166}