Skip to main content

ibridge_core/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Errors from parsing domain values and from provider calls.
4
5use std::time::Duration;
6
7/// A domain value failed validation.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
9#[non_exhaustive]
10pub enum ParseError {
11    /// The address is not a primary `@icloud.com`, `@me.com` or `@mac.com` address.
12    #[error(
13        "use the primary iCloud address (@icloud.com, @me.com or @mac.com), not a custom domain"
14    )]
15    AppleId,
16    /// The mailbox path is empty, a URL, or contains a control character.
17    #[error("mailbox path is empty, a URL, or contains a control character")]
18    MailboxPath,
19    /// An IMAP UID or UIDVALIDITY was zero.
20    #[error("uid must be a non-zero IMAP identifier")]
21    Uid,
22    /// The path is a URL, absolute, or walks out of the collection with `..`.
23    #[error("resource path must be relative to the collection, with no URL and no .. segment")]
24    ResourcePath,
25    /// The `ETag` is empty or contains a control character.
26    #[error("etag is empty or contains a control character")]
27    ETag,
28}
29
30/// An opaque id could not be decoded.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
32#[non_exhaustive]
33pub enum IdError {
34    /// The token is not a versioned, length-prefixed id, or its payload is not allowed.
35    #[error("id could not be decoded")]
36    Decode,
37}
38
39/// Failure from a mail, calendar, reminders or contacts provider.
40///
41/// [`Display`] never includes protocol detail. Callers must not put a secret in
42/// any variant. [`CoreError::protocol_detail`] is the only way to read a protocol
43/// string, and it is for diagnostics after the caller has checked it is safe.
44#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
45#[non_exhaustive]
46pub enum CoreError {
47    /// The service rejected the credentials.
48    #[error("authentication failed")]
49    Auth,
50    /// The network failed before a protocol result arrived.
51    #[error("network error")]
52    Network,
53    /// A protocol failure. The string is not part of [`Display`].
54    #[error("protocol error")]
55    Protocol(String),
56    /// The mailbox, message or resource does not exist.
57    #[error("not found")]
58    NotFound,
59    /// A conditional write lost a race with another client.
60    #[error("conflict")]
61    Conflict,
62    /// The service refused a change the signed-in account may not make.
63    ///
64    /// For example a write to a read-only shared calendar. The password was accepted, so
65    /// this is not [`CoreError::Auth`].
66    #[error("forbidden")]
67    Forbidden,
68    /// The service asked us to slow down.
69    #[error("rate limited")]
70    RateLimited {
71        /// When the caller may retry. `None` when the server did not say.
72        retry_after: Option<Duration>,
73    },
74    /// The service cannot do this operation.
75    #[error("unsupported: {0}")]
76    Unsupported(&'static str),
77    /// The host is not on the iCloud allowlist.
78    #[error("host not allowed")]
79    HostNotAllowed,
80    /// A tool or caller passed a value the provider cannot use.
81    ///
82    /// The string is a safe message for the user, never a secret or raw server text.
83    #[error("{0}")]
84    InvalidInput(String),
85}
86
87impl CoreError {
88    /// Protocol detail, when this is [`CoreError::Protocol`].
89    ///
90    /// This is not included in [`Display`].
91    #[must_use]
92    pub fn protocol_detail(&self) -> Option<&str> {
93        match self {
94            Self::Protocol(detail) => Some(detail),
95            _ => None,
96        }
97    }
98}