Skip to main content

proton_sdk/
error.rs

1//! Error types for the core SDK.
2
3use crate::api::{HumanVerification, ResponseCode};
4
5pub type Result<T> = std::result::Result<T, ProtonError>;
6
7/// Top-level error for the core SDK.
8#[derive(Debug, thiserror::Error)]
9pub enum ProtonError {
10    /// The API returned a non-success response envelope or HTTP status.
11    #[error(transparent)]
12    Api(#[from] ProtonApiError),
13
14    /// Transport-level failure (DNS, TLS, timeout, connection reset).
15    #[error("HTTP transport error: {0}")]
16    Transport(#[from] reqwest::Error),
17
18    /// Failed to (de)serialize a request or response body.
19    #[error("serialization error: {0}")]
20    Serialization(#[from] serde_json::Error),
21
22    /// A cryptographic operation failed.
23    #[error("cryptography error: {0}")]
24    Crypto(#[from] crate::crypto::CryptoError),
25
26    /// The SDK was used in a way that violates an invariant.
27    #[error("invalid operation: {0}")]
28    InvalidOperation(String),
29}
30
31impl ProtonError {
32    pub fn invalid_operation(message: impl Into<String>) -> Self {
33        Self::InvalidOperation(message.into())
34    }
35}
36
37/// An error reported by the Proton API in its response envelope.
38#[derive(Debug, Clone, thiserror::Error)]
39#[error("proton api error {code:?} (http {http_status}): {message}")]
40pub struct ProtonApiError {
41    /// Application-level response code from the `Code` field.
42    pub code: ResponseCode,
43    /// HTTP status code of the response.
44    pub http_status: u16,
45    /// Human-readable message from the `Error` field, if present.
46    pub message: String,
47    /// Raw `Details` object from the error envelope, when present. Endpoint
48    /// specific — e.g. a revision-creation conflict names the existing draft.
49    pub details: Option<serde_json::Value>,
50}
51
52impl ProtonApiError {
53    pub fn is_unauthorized(&self) -> bool {
54        self.http_status == 401
55    }
56
57    pub fn is_invalid_refresh_token(&self) -> bool {
58        matches!(self.code, ResponseCode::InvalidRefreshToken)
59    }
60
61    /// The token lacks a scope the endpoint requires — no amount of refreshing
62    /// fixes it; the user must authenticate with their password again.
63    pub fn is_insufficient_scope(&self) -> bool {
64        matches!(self.code, ResponseCode::InsufficientScope)
65    }
66
67    /// The request was gated behind human verification.
68    pub fn is_human_verification_required(&self) -> bool {
69        matches!(self.code, ResponseCode::HumanVerificationRequired)
70    }
71
72    /// The human-verification challenge, when this error carries one.
73    ///
74    /// `None` if the code is something else, or if the server gated the request
75    /// without describing how to satisfy it — which is not recoverable in-app
76    /// and should surface as a plain failure rather than an empty webview.
77    pub fn human_verification(&self) -> Option<HumanVerification> {
78        if !self.is_human_verification_required() {
79            return None;
80        }
81        serde_json::from_value(self.details.clone()?).ok()
82    }
83}