From cfad2cf13d3f790255f6617930f77756adc48bcb Mon Sep 17 00:00:00 2001
From: David Trattnig <david@subsquare.at>
Date: Fri, 21 Jan 2022 19:32:46 +0100
Subject: [PATCH] Less verbose logging when API fails. #33

---
 src/plugins/monitor.py      | 24 +++++++++++++++-----
 src/plugins/trackservice.py | 45 +++++++++++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/src/plugins/monitor.py b/src/plugins/monitor.py
index 87ecdcf2..d6227ea4 100644
--- a/src/plugins/monitor.py
+++ b/src/plugins/monitor.py
@@ -197,16 +197,28 @@ class AuraMonitor:
         body["is_healthy"] = is_healthy
         body["details"] = json.dumps(data, default=str)
         json_data = json.dumps(body, default=str)
-
         url = self.config.get("api_engine_store_health")
         url = url.replace("${ENGINE_NUMBER}", str(self.config.get("api_engine_number")))
         headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json_data, headers=headers)
+        response = requests.Response()
+        response.status_code = 404
 
-        if r.status_code == 204:
-            self.logger.info("Successfully posted healthy=%s state to Engine API!" % is_healthy)
-        else:
-            self.logger.error("HTTP %s | Error while pushing health state to Engine API: %s" % (r.status_code, str(r.json())))
+        try:
+            response = requests.post(url, data=json_data, headers=headers)
+            if response.status_code == 204:
+                self.logger.info("Successfully posted healthy=%s state to Engine API!" % is_healthy)
+            else:
+                msg = SU.red(f"HTTP {response.status_code} | Error while pushing health state to Engine API: {response.json()}")
+                self.logger.error(msg)
+        except requests.exceptions.ConnectionError:
+            self.logger.error(SU.red(f"Bad Request when posting health-status to {url}"))
+            return response
+        except requests.exceptions.Timeout:
+            self.logger.error(SU.red(f"Timeout when posting health-status to {url}"))
+            return response
+        except:
+            self.logger.error(SU.red(f"Unknown Exception when posting health-status to {url}"))
+            return response
 
 
     def update_status(self):
diff --git a/src/plugins/trackservice.py b/src/plugins/trackservice.py
index df8140e2..ca2ac32b 100644
--- a/src/plugins/trackservice.py
+++ b/src/plugins/trackservice.py
@@ -162,16 +162,30 @@ class TrackServiceHandler():
         Posts the given `PlaylistEntry` to the Engine API Playlog.
         """
         data = SU.clean_dictionary(data)
-
         self.logger.info("Posting playlog to Engine API...")
         url = self.config.get("api_engine_store_playlog")
         headers = {'content-type': 'application/json'}
         body = json.dumps(data, indent=4, sort_keys=True, default=str)
         self.logger.debug("Playlog Data: " + body)
-        response = requests.post(url, data=body, headers=headers)
-        if response.status_code != 204 or response.status_code != 204:
-            msg = f"Error while posting playlog to Engine API: {response.reason} (Error {response.status_code})\n"
-            self.logger.info(SU.red(msg) + response.content.decode("utf-8"))
+        response = requests.Response()
+        response.status_code = 404
+
+        try:
+            response = requests.post(url, data=body, headers=headers)
+            if response.status_code != 204:
+                msg = f"Error while posting playlog to Engine API: {response.reason} (Error {response.status_code})\n"
+                self.logger.info(SU.red(msg) + response.content.decode("utf-8"))
+        except requests.exceptions.ConnectionError:
+            self.logger.error(SU.red(f"Bad Request when posting playlog to {url}"))
+            return response
+        except requests.exceptions.Timeout:
+            self.logger.error(SU.red(f"Timeout when posting playlog to {url}"))
+            return response
+        except:
+            self.logger.error(SU.red(f"Unknown Exception when posting playlog to {url}"))
+            return response
+
+
 
 
     def store_clock_info(self, data):
@@ -218,10 +232,23 @@ class TrackServiceHandler():
         headers = {'content-type': 'application/json'}
         body = json.dumps(data, indent=4, sort_keys=True, default=str)
         self.logger.debug("Clock Data: " + body)
-        response = requests.put(url, data=body, headers=headers)
-        if response.status_code != 204 or response.status_code != 204:
-            msg = f"Error while posting clock-info to Engine API: {response.reason} (Error {response.status_code})\n"
-            self.logger.info(SU.red(msg) + response.content.decode("utf-8"))
+        response = requests.Response()
+        response.status_code = 404
+
+        try:
+            response = requests.put(url, data=body, headers=headers)
+            if response.status_code != 204:
+                msg = f"Error while posting clock-info to Engine API: {response.reason} (Error {response.status_code})\n"
+                self.logger.info(SU.red(msg) + response.content.decode("utf-8"))
+        except requests.exceptions.ConnectionError:
+            self.logger.error(SU.red(f"Bad Request when posting clock-info to {url}"))
+            return response
+        except requests.exceptions.Timeout:
+            self.logger.error(SU.red(f"Timeout when posting clock-info to {url}"))
+            return response
+        except:
+            self.logger.error(SU.red(f"Unknown Exception when posting clock-info to {url}"))
+            return response
 
 
 
-- 
GitLab