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>, 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]; let mut data_buffer = [0u8; 256];
loop { loop {
@@ -75,26 +70,25 @@ async fn handler(
match operation { match operation {
ConfigOperationMessage::SetWifiCreds(value, resp) => { ConfigOperationMessage::SetWifiCreds(value, resp) => {
let mut buf = [0u8; 256]; let mut f = async || {
if let Err(err) = to_slice(&value, &mut buf) { let mut buf = [0u8; 256];
let _ = resp.send(Err(err.into())).map_err(resp_fail); to_slice(&value, &mut buf)?;
continue;
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 resp.send(f().await).is_err() {
if let Err(err) = storage // TODO: find a way to not hard-code symbols in a string
.store_item(&mut data_buffer, &ConfigKey::Wifi, &buf) error!("ConfigOperationMessage::SetWifiCreds failed to respond")
.await
{
let _ = resp.send(Err(err.into())).map_err(resp_fail);
continue;
}; };
// 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);
} }
} }
} }