]> git.mxchange.org Git - fba.git/blobdiff - fba/instances.py
WIP(?):
[fba.git] / fba / instances.py
index 903fc3c6df55ab9c7212d0590b6e8c477c2bf0cf..7e3d2012949f18fbef5c0c5da28842693493fd71 100644 (file)
@@ -24,6 +24,7 @@ import validators
 from fba import blacklist
 from fba import cache
 from fba import fba
+from fba import federation
 
 # Found info from node, such as nodeinfo URL, detection mode that needs to be
 # written to database. Both arrays must be filled at the same time or else
@@ -197,9 +198,10 @@ def add(domain: str, origin: str, command: str, path: str = None):
         raise Exception(f"domain='{domain}' is a single user")
 
     # DEBUG: print("DEBUG: domain,origin,command,path:", domain, origin, command, path)
-    software = fba.determine_software(domain, path)
+    software = federation.determine_software(domain, path)
+
     # DEBUG: print("DEBUG: Determined software:", software)
-    if domain.find("/c/") > 0 and software == "lemmy":
+    if software == "lemmy" and domain.find("/c/") > 0:
         domain = domain.split("/c/")[0]
         if is_registered(domain):
             print(f"WARNING: domain='{domain}' already registered after cutting off user part. - EXIT!")
@@ -226,16 +228,10 @@ def add(domain: str, origin: str, command: str, path: str = None):
             set_data("last_status_code"  , domain, None)
             set_data("last_error_details", domain, None)
             update_data(domain)
-            fba.remove_pending_error(domain)
-
-        if domain in fba.pending_errors:
-            # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
-            update_last_error(domain, fba.pending_errors[domain])
-            fba.remove_pending_error(domain)
 
     except BaseException as exception:
-        print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
-        sys.exit(255)
+        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)
@@ -259,32 +255,36 @@ def update_last_nodeinfo(domain: str):
 
     # DEBUG: print("DEBUG: EXIT!")
 
-def update_last_error(domain: str, response: requests.models.Response):
-    # DEBUG: print("DEBUG: domain,response[]:", domain, type(response))
+def update_last_error(domain: str, error: dict):
+    # DEBUG: print("DEBUG: domain,error[]:", domain, type(error))
     if not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
     elif domain == "":
         raise ValueError("Parameter 'domain' is empty")
 
-    # DEBUG: print("DEBUG: BEFORE response[]:", type(response))
-    if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
-        response = f"response[{type(response)}]='{str(response)}'"
+    # DEBUG: print("DEBUG: BEFORE error[]:", type(error))
+    if isinstance(error, BaseException) or isinstance(error, json.decoder.JSONDecodeError):
+        error = f"error[{type(error)}]='{str(error)}'"
+    # DEBUG: print("DEBUG: AFTER error[]:", type(error))
 
-    # DEBUG: print("DEBUG: AFTER response[]:", type(response))
-    if isinstance(response, str):
-        # DEBUG: print(f"DEBUG: Setting last_error_details='{response}'")
+    if isinstance(error, str):
+        # DEBUG: print(f"DEBUG: Setting last_error_details='{error}'")
         set_data("last_status_code"  , domain, 999)
-        set_data("last_error_details", domain, response)
+        set_data("last_error_details", domain, error)
+    elif isinstance(error, requests.models.Response):
+        # DEBUG: print(f"DEBUG: Setting last_error_details='{error.reason}'")
+        set_data("last_status_code"  , domain, error.status_code)
+        set_data("last_error_details", domain, error.reason)
     else:
-        # DEBUG: print(f"DEBUG: Setting last_error_details='{response.reason}'")
-        set_data("last_status_code"  , domain, response.status_code)
-        set_data("last_error_details", domain, response.reason)
+        # DEBUG: print(f"DEBUG: Setting last_error_details='{error['error_message']}'")
+        set_data("last_status_code"  , domain, error["status_code"])
+        set_data("last_error_details", domain, error["error_message"])
 
     # Running pending updated
     # DEBUG: print(f"DEBUG: Invoking update_data({domain}) ...")
     update_data(domain)
 
-    fba.log_error(domain, response)
+    fba.log_error(domain, error)
 
     # DEBUG: print("DEBUG: EXIT!")
 
@@ -304,8 +304,8 @@ def is_registered(domain: str) -> bool:
             # Check Set all
             cache.set_all("is_registered", fba.cursor.fetchall(), True)
         except BaseException as exception:
-            print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
-            sys.exit(255)
+            update_last_error(domain, exception)
+            raise Exception(f"ERROR: failed SQL query: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'") from exception
 
     # Is cache found?
     registered = cache.sub_key_exists("is_registered", domain)