Skip to content

Commit

Permalink
Handle error in GatewayInfoProto converting
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Nov 12, 2024
1 parent e1bf9f4 commit 2eb7276
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions mobile_config/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 22 additions & 5 deletions mobile_config/src/gateway_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GatewayInfoProto> for GatewayInfo {
type Error = std::num::ParseIntError;
type Error = GatewayInfoProtoParseError;

fn try_from(info: GatewayInfoProto) -> Result<Self, Self::Error> {
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::<Utc>::from_timestamp(info.created_at as i64, 0).unwrap();
let refreshed_at = DateTime::<Utc>::from_timestamp(info.refreshed_at as i64, 0).unwrap();
let created_at = DateTime::<Utc>::from_timestamp(info.created_at as i64, 0).ok_or(
GatewayInfoProtoParseError::InvalidCreatedAt(info.created_at),
)?;

let refreshed_at = DateTime::<Utc>::from_timestamp(info.refreshed_at as i64, 0).ok_or(
GatewayInfoProtoParseError::InvalidRefreshedAt(info.refreshed_at),
)?;

Ok(Self {
address: info.address.into(),
Expand Down

0 comments on commit 2eb7276

Please sign in to comment.