From d80b9d939b6b38943ce81ec94c16801c3bfb4fef Mon Sep 17 00:00:00 2001
From: Cyril Danilevski <cyril.danilevski@xfel.eu>
Date: Fri, 24 Jan 2025 19:10:40 +0100
Subject: [PATCH] Return appropriate http codes on failure

---
 powerproc.cpp |  4 ++--
 rest.cpp      | 13 ++++++-------
 2 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/powerproc.cpp b/powerproc.cpp
index b43eb80..2001d75 100644
--- a/powerproc.cpp
+++ b/powerproc.cpp
@@ -42,12 +42,12 @@ String PowerProcedure::toJSON() {
             json += mpod.toJSON();
             json += ",\n";
         }
-        json.remove(json.length() - 2, 1);  // Remove trailing comma of last entry
+        json.remove(json.lastIndexOf(','), 1);  // Remove trailing comma of last entry
 
         json += "]\n";
         json += "},\n";
     }
-    json.remove(json.length() - 2, 1);  // Remove trailing comma of last entry
+    json.remove(json.lastIndexOf(','), 1);  // Remove trailing comma of last entry
 
     json += "]\n";  // groups body
     json += "}";    // json body
diff --git a/rest.cpp b/rest.cpp
index 384d135..3991f0c 100644
--- a/rest.cpp
+++ b/rest.cpp
@@ -84,7 +84,7 @@ void identify() {
 
     message += "\n}";
 
-    restServer.send(200, "text/json", message);
+    restServer.send(418, "text/json", message);
 }
 
 void panel() {
@@ -162,7 +162,7 @@ void sendSNMP() {
 
     http_msg += "\n}";
 
-    restServer.send(200, "text/json", http_msg);
+    restServer.send(success ? 200 : 406, "text/json", http_msg);
 }
 
 void powerGroup() {
@@ -190,18 +190,17 @@ void powerGroup() {
     }
 
     String http_msg = "{\n";
-    http_msg += "\"target\":\"" + pproc.ipAddr.toString() + "\",\n";
     http_msg += "\"arguments\":{";
     http_msg += "\"group\":\"" + group + "\",";
     http_msg += "\"output\":\"" + output + "\"},\n";
-    http_msg += "\"success\":" + String(success) + ",\n";
+    http_msg += "\"success\":" + String(success);
     if (success) {
-        http_msg += "\"status\":\n";
+        http_msg += ",\n\"status\":\n";
         http_msg += pproc.toJSON();
     }
     http_msg += "\n}";
 
-    restServer.send(200, "text/json", http_msg);
+    restServer.send(success ? 200 : 406, "text/json", http_msg);
 }
 
 void pollMPODChannel() {
@@ -236,5 +235,5 @@ void pollMPODChannel() {
     http_msg += channel;
     http_msg += "\n}";
 
-    restServer.send(200, "text/json", http_msg);
+    restServer.send(channel ? 200 : 406, "text/json", http_msg);
 }
-- 
GitLab