Skip to main content

adk_surface/mobile/
error.rs

1//! Errors of the phone (spec 3.3 §5.4).
2
3use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5use crate::error::SurfaceError;
6
7/// Why a phone route, a push or a passkey check failed (spec 3.3 §5.4, §8.2).
8///
9/// Converts into [`AdkError`] with a `surface.phone.*` code, or the engine's own code for an
10/// engine refusal; the phone's routes answer with its problem document and HTTP status.
11///
12/// # Example
13///
14/// ```rust
15/// use adk_surface::mobile::MobileError;
16///
17/// let error = adk_core::AdkError::from(MobileError::NeedsHomeUrl);
18/// assert_eq!(error.code, "surface.phone.needs_home_url");
19/// assert_eq!(error.http_status_code(), 400);
20/// ```
21#[derive(Debug, thiserror::Error)]
22pub enum MobileError {
23    /// The server was started without the home's public URL, which push, passkeys and
24    /// pairing links need.
25    #[error(
26        "the phone needs the home's public https URL: start adk-rust up with --home-url \
27         https://<host>"
28    )]
29    NeedsHomeUrl,
30    /// A push subscription points at a service the allowlist does not name, or not over
31    /// https.
32    #[error(
33        "this push endpoint is refused: {reason}. A phone subscribes through its browser's push \
34         service (Apple, Google, Mozilla or Microsoft) over https"
35    )]
36    PushEndpoint {
37        /// Why.
38        reason: String,
39    },
40    /// The push key could not be read, created or used.
41    #[error(
42        "the push key is unusable: {reason}. Delete the key file to create a new one; phones then \
43         allow notifications again"
44    )]
45    PushKey {
46        /// Why.
47        reason: String,
48    },
49    /// Confirm it's you needs a passkey verifier this server does not have.
50    #[error(
51        "confirm it's you is not available on this server yet; approve high-risk calls on a \
52         paired local terminal"
53    )]
54    PasskeyUnavailable,
55    /// A passkey registration or assertion did not verify.
56    #[error(
57        "the passkey check failed: {reason}. Try again, or approve in the home on your computer"
58    )]
59    PasskeyRefused {
60        /// Why, without secrets.
61        reason: String,
62    },
63    /// A phone registers its passkey only shortly after pairing.
64    #[error(
65        "a phone sets up its passkey within 10 minutes of pairing: remove the phone and pair it \
66         again"
67    )]
68    RegistrationClosed,
69    /// The phone already has its passkey.
70    #[error("this phone already has its passkey: remove the phone and pair it again to replace it")]
71    PasskeyExists,
72    /// The caller has no passkey to confirm with yet (spec 3.3 §8.2).
73    #[error("{hint}")]
74    NoPasskey {
75        /// Where the caller sets one up, or approves instead.
76        hint: &'static str,
77    },
78    /// The confirmation was not started here, was used, expired or belongs to another answer.
79    #[error(
80        "this confirmation is used, expired or for another answer: tap the answer again to start \
81         a new one"
82    )]
83    Challenge,
84    /// The QR code's URL is not one of this home's.
85    #[error("a pairing QR code only holds a link into this home ({home})")]
86    QrUrl {
87        /// The home URL links must start with.
88        home: String,
89    },
90    /// The engine refused or failed.
91    #[error(transparent)]
92    Surface(#[from] SurfaceError),
93    /// A store or the kernel failed.
94    #[error(transparent)]
95    Source(#[from] AdkError),
96}
97
98impl From<MobileError> for AdkError {
99    fn from(error: MobileError) -> Self {
100        let (category, code) = match &error {
101            MobileError::Surface(_) | MobileError::Source(_) => {
102                return match error {
103                    MobileError::Surface(inner) => AdkError::from(inner),
104                    MobileError::Source(inner) => inner,
105                    _ => unreachable!("matched above"),
106                };
107            }
108            MobileError::NeedsHomeUrl => {
109                (ErrorCategory::InvalidInput, "surface.phone.needs_home_url")
110            }
111            MobileError::PushEndpoint { .. } => {
112                (ErrorCategory::InvalidInput, "surface.phone.push_endpoint")
113            }
114            MobileError::PushKey { .. } => (ErrorCategory::Internal, "surface.phone.push_key"),
115            MobileError::PasskeyUnavailable => {
116                (ErrorCategory::Unsupported, "surface.phone.passkey_unavailable")
117            }
118            MobileError::PasskeyRefused { .. } => {
119                (ErrorCategory::Forbidden, "surface.phone.passkey_refused")
120            }
121            MobileError::RegistrationClosed => {
122                (ErrorCategory::Forbidden, "surface.phone.registration_closed")
123            }
124            MobileError::PasskeyExists => (ErrorCategory::Conflict, "surface.phone.passkey_exists"),
125            MobileError::NoPasskey { .. } => (ErrorCategory::Forbidden, "surface.phone.no_passkey"),
126            MobileError::Challenge => (ErrorCategory::Forbidden, "surface.phone.challenge"),
127            MobileError::QrUrl { .. } => (ErrorCategory::InvalidInput, "surface.phone.qr_url"),
128        };
129        AdkError::new(ErrorComponent::Server, category, code, error.to_string())
130    }
131}