]> the.earth.li Git - mqttdeck.git/commitdiff
Handle JSON parsing failures more gracefully
authorJonathan McDowell <noodles@earth.li>
Sat, 26 Apr 2025 18:24:58 +0000 (19:24 +0100)
committerJonathan McDowell <noodles@earth.li>
Sat, 26 Apr 2025 18:24:58 +0000 (19:24 +0100)
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

index 639c4dae40ca19e06f629dc6b9082e80367c3b41..978a77b4b7e7620cc9909214201a2527966c37b9 100644 (file)
@@ -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;