1use crate::api::ResponseCode;
4
5pub type Result<T> = std::result::Result<T, ProtonError>;
6
7#[derive(Debug, thiserror::Error)]
9pub enum ProtonError {
10 #[error(transparent)]
12 Api(#[from] ProtonApiError),
13
14 #[error("HTTP transport error: {0}")]
16 Transport(#[from] reqwest::Error),
17
18 #[error("serialization error: {0}")]
20 Serialization(#[from] serde_json::Error),
21
22 #[error("cryptography error: {0}")]
24 Crypto(#[from] crate::crypto::CryptoError),
25
26 #[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#[derive(Debug, Clone, thiserror::Error)]
39#[error("proton api error {code:?} (http {http_status}): {message}")]
40pub struct ProtonApiError {
41 pub code: ResponseCode,
43 pub http_status: u16,
45 pub message: String,
47}
48
49impl ProtonApiError {
50 pub fn is_unauthorized(&self) -> bool {
51 self.http_status == 401
52 }
53
54 pub fn is_invalid_refresh_token(&self) -> bool {
55 matches!(self.code, ResponseCode::InvalidRefreshToken)
56 }
57
58 pub fn is_insufficient_scope(&self) -> bool {
61 matches!(self.code, ResponseCode::InsufficientScope)
62 }
63}