refactor(config): use an async closure instead of multiple if statements for error handling

This commit is contained in:
2026-10-08 16:45:29 -05:00
parent a4acfc0915
commit 83b0b85fee
+17 -23
View File
@@ -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 buf = [0u8; 256];
if let Err(err) = to_slice(&value, &mut buf) {
let _ = resp.send(Err(err.into())).map_err(resp_fail);
continue;
let mut f = async || {
let mut buf = [0u8; 256];
to_slice(&value, &mut buf)?;
storage
.store_item(&mut data_buffer, &ConfigKey::Wifi, &buf)
.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;
Ok(())
};
if let Err(err) = storage
.store_item(&mut data_buffer, &ConfigKey::Wifi, &buf)
.await
{
let _ = resp.send(Err(err.into())).map_err(resp_fail);
continue;
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")
};
// 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);
}
}
}