From c6ec22a983fd1f38fc2171ede611e23cfd527320 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Sat, 21 Sep 2024 20:37:16 +0930 Subject: [PATCH] Better handling of error responses from Sondehub --- auto_rx/autorx/__init__.py | 2 +- auto_rx/autorx/sondehub.py | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/auto_rx/autorx/__init__.py b/auto_rx/autorx/__init__.py index b9036fd6..fbf0408f 100644 --- a/auto_rx/autorx/__init__.py +++ b/auto_rx/autorx/__init__.py @@ -12,7 +12,7 @@ # MINOR - New sonde type support, other fairly big changes that may result in telemetry or config file incompatability issus. # PATCH - Small changes, or minor feature additions. -__version__ = "1.7.5-beta6" +__version__ = "1.7.5-beta7" # Global Variables diff --git a/auto_rx/autorx/sondehub.py b/auto_rx/autorx/sondehub.py index 70cfd388..a5e6d961 100644 --- a/auto_rx/autorx/sondehub.py +++ b/auto_rx/autorx/sondehub.py @@ -451,10 +451,28 @@ def upload_telemetry(self, telem_list): continue elif (_req.status_code == 201) or (_req.status_code == 202): - self.log_debug( - "Sondehub reported issue when adding packets to DB. Status Code: %d %s." - % (_req.status_code, _req.text) - ) + # A 202 return code means there was some kind of data issue. + # We expect a response of the form {"message": "error message", "errors":[], "warnings":[]} + try: + _resp_json = _req.json() + + for _error in _resp_json['errors']: + if 'z-check' not in _error["error_message"]: + self.log_error("Payload data error: " + _error["error_message"]) + else: + self.log_debug("Payload data error: " + _error["error_message"]) + if 'payload' in _error: + self.log_debug("Payload data associated with error: " + str(_error['payload'])) + + for _warning in _resp_json['warnings']: + self.log_warning("Payload data warning: " + _warning["warning_message"]) + if 'payload' in _warning: + self.log_debug("Payload data associated with warning: " + str(_warning['payload'])) + + except Exception as e: + self.log_error("Error when parsing 202 response as JSON: %s" % str(e)) + self.log_debug("Content of 202 response: %s" % _req.text) + _upload_success = True break @@ -576,6 +594,12 @@ def log_error(self, line): """ logging.error("Sondehub Uploader - %s" % line) + def log_warning(self, line): + """ Helper function to log an error message with a descriptive heading. + Args: + line (str): Message to be logged. + """ + logging.warning("Sondehub Uploader - %s" % line) if __name__ == "__main__": # Test Script