From e1c85642bdcc3a8ba614fe2961de6a7611d7a4fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 11 Jun 2023 17:42:06 +0200 Subject: [PATCH] Continued: - always transport causing exception - some places may still handle it --- fba/commands.py | 7 +++--- fba/federation.py | 16 +++++++++---- fba/instances.py | 61 ++++++++++++++++++++--------------------------- fba/network.py | 3 +++ 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/fba/commands.py b/fba/commands.py index f84ce6f..d71c0ae 100644 --- a/fba/commands.py +++ b/fba/commands.py @@ -440,7 +440,7 @@ def fetch_fbabot_atom(args: argparse.Namespace): # DEBUG: print("DEBUG: EXIT!") -def fetch_instances(args: argparse.Namespace): +def fetch_instances(args: argparse.Namespace) -> int: # DEBUG: print(f"DEBUG: args[]='{type(args)}' - CALLED!") locking.acquire() @@ -451,11 +451,11 @@ def fetch_instances(args: argparse.Namespace): except network.exceptions as exception: print(f"WARNING: Exception '{type(exception)}' during fetching instances from args.domain='{args.domain}'") instances.update_last_error(args.domain, exception) - return + return 100 if args.single: # DEBUG: print("DEBUG: Not fetching more instances - EXIT!") - return + return 0 # Loop through some instances fba.cursor.execute( @@ -478,6 +478,7 @@ def fetch_instances(args: argparse.Namespace): instances.update_last_error(row[0], exception) # DEBUG: print("DEBUG: EXIT!") + return 0 def fetch_federater(args: argparse.Namespace): # DEBUG: print(f"DEBUG: args[]='{type(args)}' - CALLED!") diff --git a/fba/federation.py b/fba/federation.py index ee839c7..1e40052 100644 --- a/fba/federation.py +++ b/fba/federation.py @@ -200,7 +200,11 @@ def fetch_nodeinfo(domain: str, path: str = None) -> dict: headers = csrf.determine(domain, dict()) except network.exceptions as exception: print(f"WARNING: Exception '{type(exception)}' during checking CSRF (nodeinfo,{__name__}) - EXIT!") - return dict() + return { + "status_code" : 500, + "error_message": f"exception[{type(exception)}]='{str(exception)}'", + "exception" : exception, + } request_paths = [ "/nodeinfo/2.1.json", @@ -254,7 +258,8 @@ def fetch_wellknown_nodeinfo(domain: str) -> dict: print(f"WARNING: Exception '{type(exception)}' during checking CSRF (fetch_wellknown,{__name__}) - EXIT!") return { "status_code" : 500, - "error_message": type(exception) + "error_message": type(exception), + "exception" : exception, } # DEBUG: print("DEBUG: Fetching .well-known info for domain:", domain) @@ -375,8 +380,11 @@ def determine_software(domain: str, path: str = None) -> str: # DEBUG: print(f"DEBUG: data[{type(data)}]='{data}'") if "error_message" in data: - # DEBUG: print("DEBUG: Could not determine software type:", domain) - return fetch_generator_from_path(domain) + # DEBUG: print(f"DEBUG: Could not determine software type, domain='{domain}'") + if "exception" in data: + raise Exception(f"Cannot fetch nodeinfo from domain='{domain}': '{data['error_message']}'") from data["exception"] + else: + raise Exception(f"Cannot fetch nodeinfo from domain='{domain}': '{data['error_message']}'") # DEBUG: print("DEBUG: data():", len(data), data) if "status" in data["json"] and data["json"]["status"] == "error" and "message" in data["json"]: diff --git a/fba/instances.py b/fba/instances.py index 039661d..e2da016 100644 --- a/fba/instances.py +++ b/fba/instances.py @@ -206,33 +206,28 @@ def add(domain: str, origin: str, command: str, path: str = None): return print(f"INFO: Adding instance domain='{domain}' (origin='{origin}',software='{software}')") - try: - fba.cursor.execute( - "INSERT INTO instances (domain, origin, command, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)", - ( - domain, - origin, - command, - fba.get_hash(domain), - software, - time.time() - ), - ) - - cache.set_sub_key("is_registered", domain, True) - - if has_pending_instance_data(domain): - # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...") - set_data("last_status_code" , domain, None) - set_data("last_error_details", domain, None) - update_data(domain) - - except BaseException as exception: - update_last_error(domain, exception) - raise Exception(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'") from exception - else: - # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain) - update_last_nodeinfo(domain) + fba.cursor.execute( + "INSERT INTO instances (domain, origin, command, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)", + ( + domain, + origin, + command, + fba.get_hash(domain), + software, + time.time() + ), + ) + + cache.set_sub_key("is_registered", domain, True) + + if has_pending_instance_data(domain): + # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...") + set_data("last_status_code" , domain, None) + set_data("last_error_details", domain, None) + update_data(domain) + + # DEBUG: print(f"DEBUG: Updating nodeinfo for domain='{domain}'") + update_last_nodeinfo(domain) # DEBUG: print("DEBUG: EXIT!") @@ -301,14 +296,10 @@ def is_registered(domain: str) -> bool: # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!") if not cache.key_exists("is_registered"): # DEBUG: print("DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...") - try: - fba.cursor.execute("SELECT domain FROM instances") - - # Check Set all - cache.set_all("is_registered", fba.cursor.fetchall(), True) - except BaseException as exception: - update_last_error(domain, exception) - raise Exception(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'") from exception + fba.cursor.execute("SELECT domain FROM instances") + + # Check Set all + cache.set_all("is_registered", fba.cursor.fetchall(), True) # Is cache found? registered = cache.sub_key_exists("is_registered", domain) diff --git a/fba/network.py b/fba/network.py index 281afb2..bc7a95f 100644 --- a/fba/network.py +++ b/fba/network.py @@ -82,6 +82,7 @@ def post_json_api(domain: str, path: str, data: str, headers: dict = {}) -> dict # DEBUG: print(f"DEBUG: Fetching '{path}' from '{domain}' failed. exception[{type(exception)}]='{str(exception)}'") json_reply["status_code"] = 999 json_reply["error_message"] = f"exception['{type(exception)}']='{str(exception)}'" + json_reply["exception"] = exception instances.update_last_error(domain, exception) raise exception @@ -115,6 +116,7 @@ def fetch_api_url(url: str, timeout: tuple) -> dict: # DEBUG: print(f"DEBUG: Fetching '{url}' failed. exception[{type(exception)}]='{str(exception)}'") json_reply["status_code"] = 999 json_reply["error_message"] = f"exception['{type(exception)}']='{str(exception)}'" + json_reply["exception"] = exception raise exception # DEBUG: print(f"DEBUG: Returning json_reply({len(json_reply)})=[]:{type(json_reply)}") @@ -151,6 +153,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: # DEBUG: print(f"DEBUG: Fetching '{path}' from '{domain}' failed. exception[{type(exception)}]='{str(exception)}'") json_reply["status_code"] = 999 json_reply["error_message"] = f"exception['{type(exception)}']='{str(exception)}'" + json_reply["exception"] = exception instances.update_last_error(domain, exception) raise exception -- 2.39.5