Skip to content

Commit

Permalink
[RSDK-8127] on invalid config create empty robot over panicking (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvaradarajan authored Jul 24, 2024
1 parent d5f7e23 commit 6b2622d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
6 changes: 6 additions & 0 deletions micro-rdk/src/common/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum BoardError {
#[error(transparent)]
#[cfg(feature = "esp32")]
EspError(#[from] EspError),
#[error("construction error test")]
TestError,
}

pub static COMPONENT_NAME: &str = "board";
Expand Down Expand Up @@ -130,6 +132,10 @@ impl FakeBoard {
}

pub(crate) fn from_config(cfg: ConfigType) -> Result<BoardType, BoardError> {
if cfg.get_attribute::<bool>("fail_new").unwrap_or(false) {
return Err(BoardError::TestError);
}

let analogs = if let Ok(analog_confs) = cfg.get_attribute::<HashMap<&str, f64>>("analogs") {
analog_confs
.iter()
Expand Down
29 changes: 8 additions & 21 deletions micro-rdk/src/esp32/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,23 @@ pub async fn serve_web_inner<S>(
RobotRepresentation::WithRobot(robot) => Arc::new(Mutex::new(robot)),
RobotRepresentation::WithRegistry(registry) => {
log::info!("building robot from config");
let r = match LocalRobot::from_cloud_config(
let (r, err) = match LocalRobot::from_cloud_config(
exec.clone(),
app_config.get_robot_id(),
&cfg_response,
registry,
cfg_received_datetime,
) {
Ok(robot) => {
if let Some(datetime) = cfg_received_datetime {
let logs = vec![config_log_entry(datetime, None)];
app_client
.push_logs(logs)
.await
.expect("could not push logs to app");
}
robot
}
Ok(robot) => (robot, None),
Err(err) => {
if let Some(datetime) = cfg_received_datetime {
let logs = vec![config_log_entry(datetime, Some(err))];
app_client
.push_logs(logs)
.await
.expect("could not push logs to app");
}
// TODO(RSDK-8172) shouldn't panic here, when we support offline mode and
// reloading configuration this should be removed
panic!("couldn't build robot");
log::error!("could not build robot from config due to {:?}, defaulting to empty robot until a valid config is accessible", err);
(LocalRobot::new(), Some(err))
}
};
if let Some(datetime) = cfg_received_datetime {
let logs = vec![config_log_entry(datetime, err)];
let _ = app_client.push_logs(logs).await;
}
Arc::new(Mutex::new(r))
}
};
Expand Down
29 changes: 8 additions & 21 deletions micro-rdk/src/native/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,23 @@ pub async fn serve_web_inner<S>(
RobotRepresentation::WithRobot(robot) => Arc::new(Mutex::new(robot)),
RobotRepresentation::WithRegistry(registry) => {
log::info!("building robot from config");
let r = match LocalRobot::from_cloud_config(
let (r, err) = match LocalRobot::from_cloud_config(
exec.clone(),
app_config.get_robot_id(),
&cfg_response,
registry,
cfg_received_datetime,
) {
Ok(robot) => {
if let Some(datetime) = cfg_received_datetime {
let logs = vec![config_log_entry(datetime, None)];
app_client
.push_logs(logs)
.await
.expect("could not push logs to app");
}
robot
}
Ok(robot) => (robot, None),
Err(err) => {
if let Some(datetime) = cfg_received_datetime {
let logs = vec![config_log_entry(datetime, Some(err))];
app_client
.push_logs(logs)
.await
.expect("could not push logs to app");
}
// TODO(RSDK-8127) shouldn't panic here, when we support offline mode and
// reloading configuration this should be removed
panic!("couldn't build robot");
log::error!("could not build robot from config due to {:?}, defaulting to empty robot until a valid config is accessible", err);
(LocalRobot::new(), Some(err))
}
};
if let Some(datetime) = cfg_received_datetime {
let logs = vec![config_log_entry(datetime, err)];
let _ = app_client.push_logs(logs).await;
}
Arc::new(Mutex::new(r))
}
};
Expand Down

0 comments on commit 6b2622d

Please sign in to comment.