From ec3280c645d1d386cecc18917c1c2aa133e3a2ef Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Sat, 26 Apr 2025 19:24:58 +0100 Subject: [PATCH] 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. --- src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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; -- 2.39.5