From 95d6e368dc2fb1a350c5cb02c7859875440cbb65 Mon Sep 17 00:00:00 2001 From: Tim Jaacks <tim.jaacks@seco.com> Date: Mon, 12 Dec 2022 15:36:42 +0100 Subject: [PATCH] alphaplan_fwr_import: add error handling --- scripts/alphaplan_fwr_import.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/scripts/alphaplan_fwr_import.py b/scripts/alphaplan_fwr_import.py index 9583cec4..93a95d8d 100755 --- a/scripts/alphaplan_fwr_import.py +++ b/scripts/alphaplan_fwr_import.py @@ -9,12 +9,21 @@ import requests def ap_send_json(jsonobj: dict, url: str, user: str, password: str, cert_file: str): """Sends the generated files to the Alphaplan webservice""" - msg = requests.post( - url, json=jsonobj, auth=(user, password), verify=cert_file, timeout=10 - ) - msg_json = msg.json() + try: + msg = requests.post( + url, json=jsonobj, auth=(user, password), verify=cert_file, timeout=10 + ) + except requests.exceptions.RequestException as e: + sys.exit("ERROR: %s" % e) + + try: + msg_json = msg.json() + except json.decoder.JSONDecodeError: + sys.exit("ERROR: Did not receive a valid JSON reply from Alphaplan webservice") + if msg_json["Status"] != "Ok": sys.exit("ERROR: AlphaPlan webservice post request failed") + print("AlphaPlan webservice response: {}".format(msg_json["Meldung"])) @@ -62,8 +71,13 @@ def main(): for filename in files: print("Importing JSON file %s" % filename) with open(filename, "r", encoding="utf-8") as f: + try: + json_data = json.load(f) + except json.decoder.JSONDecodeError: + sys.exit("ERROR: Could not parse JSON data from %f" % filename) + ap_send_json( - json.load(f), + json_data, args.url, args.user, args.password, -- GitLab