refactor(config): use an async closure instead of multiple if statements for error handling
This commit is contained in:
+10
-16
@@ -63,11 +63,6 @@ async fn handler(
|
||||
Cache<Uncached, Uncached, Uncached, ConfigKey>,
|
||||
>,
|
||||
) -> ! {
|
||||
let resp_fail = |_| {
|
||||
// TODO: make tracing work so if this code is reached you know where the error originated
|
||||
error!("failed to respond");
|
||||
};
|
||||
|
||||
let mut data_buffer = [0u8; 256];
|
||||
|
||||
loop {
|
||||
@@ -75,26 +70,25 @@ async fn handler(
|
||||
|
||||
match operation {
|
||||
ConfigOperationMessage::SetWifiCreds(value, resp) => {
|
||||
let mut f = async || {
|
||||
let mut buf = [0u8; 256];
|
||||
if let Err(err) = to_slice(&value, &mut buf) {
|
||||
let _ = resp.send(Err(err.into())).map_err(resp_fail);
|
||||
continue;
|
||||
};
|
||||
to_slice(&value, &mut buf)?;
|
||||
|
||||
if let Err(err) = storage
|
||||
storage
|
||||
.store_item(&mut data_buffer, &ConfigKey::Wifi, &buf)
|
||||
.await
|
||||
{
|
||||
let _ = resp.send(Err(err.into())).map_err(resp_fail);
|
||||
continue;
|
||||
};
|
||||
.await?;
|
||||
|
||||
// TODO: send the change to the wifi service
|
||||
// WIFI_SERVICE
|
||||
// .call(|s| s.set_station_config(esp_radio::wifi::Config::Station(value.into())))
|
||||
// .await;
|
||||
|
||||
let _ = resp.send(Ok(())).map_err(resp_fail);
|
||||
Ok(())
|
||||
};
|
||||
if resp.send(f().await).is_err() {
|
||||
// TODO: find a way to not hard-code symbols in a string
|
||||
error!("ConfigOperationMessage::SetWifiCreds failed to respond")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user