diff --git a/mobile_config/src/client/mod.rs b/mobile_config/src/client/mod.rs index f9ecd7d18..63fafb1b6 100644 --- a/mobile_config/src/client/mod.rs +++ b/mobile_config/src/client/mod.rs @@ -27,6 +27,8 @@ pub enum ClientError { LocationParseError(#[from] std::num::ParseIntError), #[error("unknown service provider {0}")] UnknownServiceProvider(String), + #[error("Invalid GatewayInfo proto response{0}")] + InvalidGatewayInfoProto(#[from] crate::gateway_info::GatewayInfoProtoParseError), } macro_rules! call_with_retry { diff --git a/mobile_config/src/gateway_info.rs b/mobile_config/src/gateway_info.rs index 94c12c190..6ce6eb7c7 100644 --- a/mobile_config/src/gateway_info.rs +++ b/mobile_config/src/gateway_info.rs @@ -28,23 +28,40 @@ impl GatewayInfo { } } +#[derive(thiserror::Error, Debug)] +pub enum GatewayInfoProtoParseError { + #[error("Invalid location string: {0}")] + InvalidLocation(String), + #[error("Invalid created_at: {0}")] + InvalidCreatedAt(u64), + #[error("Invalid refreshed_at: {0}")] + InvalidRefreshedAt(u64), +} + impl TryFrom for GatewayInfo { - type Error = std::num::ParseIntError; + type Error = GatewayInfoProtoParseError; fn try_from(info: GatewayInfoProto) -> Result { let metadata = if let Some(ref metadata) = info.metadata { Some( u64::from_str_radix(&metadata.location, 16) - .map(|location| GatewayMetadata { location })?, + .map(|location| GatewayMetadata { location }) + .map_err(|_| { + GatewayInfoProtoParseError::InvalidLocation(metadata.location.clone()) + })?, ) } else { None }; let device_type = info.device_type().into(); - // TODO remove unwraps - let created_at = DateTime::::from_timestamp(info.created_at as i64, 0).unwrap(); - let refreshed_at = DateTime::::from_timestamp(info.refreshed_at as i64, 0).unwrap(); + let created_at = DateTime::::from_timestamp(info.created_at as i64, 0).ok_or( + GatewayInfoProtoParseError::InvalidCreatedAt(info.created_at), + )?; + + let refreshed_at = DateTime::::from_timestamp(info.refreshed_at as i64, 0).ok_or( + GatewayInfoProtoParseError::InvalidRefreshedAt(info.refreshed_at), + )?; Ok(Self { address: info.address.into(),