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