use auths_crypto::AuthsErrorInfo;
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PairingError {
#[error(transparent)]
Protocol(#[from] auths_pairing_protocol::ProtocolError),
#[error("QR code generation failed: {0}")]
QrCodeFailed(String),
#[error("Relay error: {0}")]
RelayError(String),
#[error("Local server error: {0}")]
LocalServerError(String),
#[error("mDNS error: {0}")]
MdnsError(String),
#[error("No peer found on local network")]
NoPeerFound,
#[error("LAN pairing timed out")]
LanTimeout,
}
impl AuthsErrorInfo for PairingError {
fn error_code(&self) -> &'static str {
match self {
Self::Protocol(_) => "AUTHS-E3201",
Self::QrCodeFailed(_) => "AUTHS-E3202",
Self::RelayError(_) => "AUTHS-E3203",
Self::LocalServerError(_) => "AUTHS-E3204",
Self::MdnsError(_) => "AUTHS-E3205",
Self::NoPeerFound => "AUTHS-E3206",
Self::LanTimeout => "AUTHS-E3207",
}
}
fn suggestion(&self) -> Option<&'static str> {
match self {
Self::NoPeerFound => Some("Ensure both devices are on the same network"),
Self::LanTimeout => Some("Check your network and try again"),
Self::RelayError(_) => Some("Check your internet connection"),
Self::Protocol(_) => Some("Ensure both devices are running compatible auths versions"),
Self::QrCodeFailed(_) => {
Some("QR code generation failed; try `auths device pair --mode relay` instead")
}
Self::LocalServerError(_) => {
Some("The local pairing server failed to start; check that the port is available")
}
Self::MdnsError(_) => {
Some("mDNS discovery failed; try `auths device pair --mode relay` instead")
}
}
}
}
impl From<serde_json::Error> for PairingError {
fn from(e: serde_json::Error) -> Self {
PairingError::Protocol(auths_pairing_protocol::ProtocolError::Serialization(
e.to_string(),
))
}
}