use adk_core::{AdkError, ErrorCategory, ErrorComponent};
use crate::error::SurfaceError;
#[derive(Debug, thiserror::Error)]
pub enum MobileError {
#[error(
"the phone needs the home's public https URL: start adk-rust up with --home-url \
https://<host>"
)]
NeedsHomeUrl,
#[error(
"this push endpoint is refused: {reason}. A phone subscribes through its browser's push \
service (Apple, Google, Mozilla or Microsoft) over https"
)]
PushEndpoint {
reason: String,
},
#[error(
"the push key is unusable: {reason}. Delete the key file to create a new one; phones then \
allow notifications again"
)]
PushKey {
reason: String,
},
#[error(
"confirm it's you is not available on this server yet; approve high-risk calls on a \
paired local terminal"
)]
PasskeyUnavailable,
#[error(
"the passkey check failed: {reason}. Try again, or approve in the home on your computer"
)]
PasskeyRefused {
reason: String,
},
#[error(
"a phone sets up its passkey within 10 minutes of pairing: remove the phone and pair it \
again"
)]
RegistrationClosed,
#[error("this phone already has its passkey: remove the phone and pair it again to replace it")]
PasskeyExists,
#[error("{hint}")]
NoPasskey {
hint: &'static str,
},
#[error(
"this confirmation is used, expired or for another answer: tap the answer again to start \
a new one"
)]
Challenge,
#[error("a pairing QR code only holds a link into this home ({home})")]
QrUrl {
home: String,
},
#[error(transparent)]
Surface(#[from] SurfaceError),
#[error(transparent)]
Source(#[from] AdkError),
}
impl From<MobileError> for AdkError {
fn from(error: MobileError) -> Self {
let (category, code) = match &error {
MobileError::Surface(_) | MobileError::Source(_) => {
return match error {
MobileError::Surface(inner) => AdkError::from(inner),
MobileError::Source(inner) => inner,
_ => unreachable!("matched above"),
};
}
MobileError::NeedsHomeUrl => {
(ErrorCategory::InvalidInput, "surface.phone.needs_home_url")
}
MobileError::PushEndpoint { .. } => {
(ErrorCategory::InvalidInput, "surface.phone.push_endpoint")
}
MobileError::PushKey { .. } => (ErrorCategory::Internal, "surface.phone.push_key"),
MobileError::PasskeyUnavailable => {
(ErrorCategory::Unsupported, "surface.phone.passkey_unavailable")
}
MobileError::PasskeyRefused { .. } => {
(ErrorCategory::Forbidden, "surface.phone.passkey_refused")
}
MobileError::RegistrationClosed => {
(ErrorCategory::Forbidden, "surface.phone.registration_closed")
}
MobileError::PasskeyExists => (ErrorCategory::Conflict, "surface.phone.passkey_exists"),
MobileError::NoPasskey { .. } => (ErrorCategory::Forbidden, "surface.phone.no_passkey"),
MobileError::Challenge => (ErrorCategory::Forbidden, "surface.phone.challenge"),
MobileError::QrUrl { .. } => (ErrorCategory::InvalidInput, "surface.phone.qr_url"),
};
AdkError::new(ErrorComponent::Server, category, code, error.to_string())
}
}