From: Jonathan McDowell Date: Sat, 26 Apr 2025 18:24:58 +0000 (+0100) Subject: Handle JSON parsing failures more gracefully X-Git-Tag: v0.2.0~6 X-Git-Url: https://the.earth.li/gitweb/?a=commitdiff_plain;h=ec3280c645d1d386cecc18917c1c2aa133e3a2ef;p=mqttdeck.git Handle JSON parsing failures more gracefully Add a match block so we can return an error if we get some JSON we can't decode instead of panicking our MQTT worker. --- diff --git a/src/main.rs b/src/main.rs index 639c4da..978a77b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,9 +54,16 @@ async fn handle_commands( while let Ok(msg_opt) = strm.recv().await { if let Some(msg) = msg_opt { let data = std::str::from_utf8(msg.payload()).unwrap(); - let command = json::parse(data).unwrap(); let resp_topic = msg.topic().replace("/cmnd", "/result"); + let command = match json::parse(data) { + Ok(command) => command, + Err(e) => { + send_command_response(&cli, &resp_topic, e.to_string().as_str()).await; + continue; + } + }; + if command["action"].is_null() { send_command_response(&cli, &resp_topic, "no action supplied").await; continue;