From 2b9ebdeab2399bd8b92fb37006e6b4476bb209df Mon Sep 17 00:00:00 2001 From: Timothy Miller <46549361+timothymiller@users.noreply.github.com> Date: Sat, 30 Jul 2022 20:28:54 -0400 Subject: [PATCH] Added exception handling for unhandled api requests --- cloudflare-ddns.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index dea4c69..938811a 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -170,21 +170,25 @@ def cf_api(endpoint, method, config, headers={}, data=False): "X-Auth-Email": config['authentication']['api_key']['account_email'], "X-Auth-Key": config['authentication']['api_key']['api_key'], } + try: + if(data == False): + response = requests.request( + method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers) + else: + response = requests.request( + method, "https://api.cloudflare.com/client/v4/" + endpoint, + headers=headers, json=data) - if(data == False): - response = requests.request( - method, "https://api.cloudflare.com/client/v4/" + endpoint, headers=headers) - else: - response = requests.request( - method, "https://api.cloudflare.com/client/v4/" + endpoint, - headers=headers, json=data) - - if response.ok: - return response.json() - else: - print("📈 Error sending '" + method + - "' request to '" + response.url + "':") - print(response.text) + if response.ok: + return response.json() + else: + print("😡 Error sending '" + method + + "' request to '" + response.url + "':") + print(response.text) + return None + except Exception as e: + print("😡 An exception occurred while sending '" + + method + "' request to '" + endpoint + "': " + str(e)) return None @@ -209,8 +213,8 @@ if __name__ == '__main__': config = json.loads(config_file.read()) except: print("😡 Error reading config.json") - # wait 60 seconds to prevent excessive logging on docker auto restart - time.sleep(60) + # wait 10 seconds to prevent excessive logging on docker auto restart + time.sleep(10) if config is not None: try: