From: Roland Häder Date: Mon, 27 Jan 2025 01:27:34 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6cd8470e7c1cda1f87753071abc60ef4b8894a00;p=fba.git Continued: - proper exception, not ValueException when it is a runtime exception --- diff --git a/fba/helpers/blocklists.py b/fba/helpers/blocklists.py index c208049..61467d0 100644 --- a/fba/helpers/blocklists.py +++ b/fba/helpers/blocklists.py @@ -79,7 +79,7 @@ def has(domain: str) -> bool: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") # Default is not found found = False diff --git a/fba/helpers/blocks.py b/fba/helpers/blocks.py index f522d57..58836f1 100644 --- a/fba/helpers/blocks.py +++ b/fba/helpers/blocks.py @@ -29,7 +29,7 @@ def alias_block_level(block_level: str) -> str: if not isinstance(block_level, str): raise TypeError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level in ["accept", "accepted"]: - raise ValueError(f"Parameter block_level='{block_level}' is 'accept(ed)' but function was invoked") + raise RuntimeError(f"Parameter block_level='{block_level}' is 'accept(ed)' but function was invoked") elif block_level == "silence": logger.debug("Block level 'silence' has been changed to 'silenced'") block_level = "silenced" diff --git a/fba/helpers/cookies.py b/fba/helpers/cookies.py index 6b2cbff..16c8711 100644 --- a/fba/helpers/cookies.py +++ b/fba/helpers/cookies.py @@ -31,7 +31,7 @@ def store(domain: str, cookies: dict): domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(cookies, dict): raise TypeError(f"Parameter cookies[]='{type(cookies)}' is not of type 'dict'") @@ -44,7 +44,7 @@ def get_all(domain: str) -> dict: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif domain not in _cookies: return [] @@ -56,7 +56,7 @@ def has(domain: str) -> bool: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") has_cookies = domain in _cookies @@ -68,7 +68,7 @@ def clear(domain: str): domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") if has(domain): logger.debug("Removing cookies for domain='%s' ...", domain) diff --git a/fba/helpers/domain.py b/fba/helpers/domain.py index 9094663..d3e63ef 100644 --- a/fba/helpers/domain.py +++ b/fba/helpers/domain.py @@ -60,13 +60,13 @@ def is_in_url(domain: str, url: str) -> bool: raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(url, str): raise TypeError(f"Parameter url[]='{type(url)}' is not of type 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") elif not validators.url(url): - raise ValueError(f"Parameter url='{url}' is not a valid URL") + raise RuntimeError(f"Parameter url='{url}' is not a valid URL but function was invoked") punycode = encode_idna(domain) logger.debug("punycode='%s'", punycode) diff --git a/fba/helpers/json.py b/fba/helpers/json.py index 4323b27..5851319 100644 --- a/fba/helpers/json.py +++ b/fba/helpers/json.py @@ -29,7 +29,7 @@ def is_json_response(response: requests.models.Response) -> bool: if not isinstance(response, requests.models.Response): raise TypeError(f"Parameter response[]='{type(response)}' is not type of 'Response'") elif not response.ok or response.status_code > 200: - raise ValueError(f"response.ok='{response.ok}',response.status_code={response.status_code},response.reason='{response.reason}' but function was invoked") + raise RuntimeError(f"response.ok='{response.ok}',response.status_code={response.status_code},response.reason='{response.reason}' but function was invoked") is_json = response.headers.get("content-type") is None or response.headers.get("content-type").split(";")[0] in ["*/*", "application/json", "application/jrd+json", "application/activity+json"] @@ -42,7 +42,7 @@ def from_response(response: requests.models.Response) -> any: if not isinstance(response, requests.models.Response): raise TypeError(f"Parameter response[]='{type(response)}' is not type of 'Response'") elif not response.ok or response.status_code > 200: - raise ValueError(f"response.ok='{response.ok}',response.status_code={response.status_code},response.reason='{response.reason}' but function was invoked") + raise RuntimeError(f"response.ok='{response.ok}',response.status_code={response.status_code},response.reason='{response.reason}' but function was invoked") elif response.text.strip() != "" and not is_json_response(response): logger.warning("response.headers[content-type]='%s' is not a JSON type, below json() invocation may raise an exception", response.headers.get("content-type")) diff --git a/fba/helpers/processing.py b/fba/helpers/processing.py index 0acec17..9349694 100644 --- a/fba/helpers/processing.py +++ b/fba/helpers/processing.py @@ -48,9 +48,9 @@ def instance(blocked: str, blocker: str, command: str, force: bool = False) -> b elif command == "": raise ValueError("Parameter 'command' is empty") elif blacklist.is_blacklisted(blocked): - raise ValueError(f"blocked='{blocked}' is blacklisted but function was invoked") + raise RuntimeError(f"blocked='{blocked}' is blacklisted but function was invoked") elif blacklist.is_blacklisted(blocker): - raise ValueError(f"blocker='{blocker}' is blacklisted but function was invoked") + raise RuntimeError(f"blocker='{blocker}' is blacklisted but function was invoked") logger.debug("blocked='%s' - BEFORE!", blocked) blocked = utils.deobfuscate(blocked, blocker) @@ -102,9 +102,9 @@ def block(blocker: str, blocked: str, reason: str, block_level: str) -> bool: elif block_level in ["reject", "suspend", "accept", "silence", "nsfw", "quarantined_instances"]: raise ValueError(f"Parameter block_level='{block_level}' is not supported") elif blacklist.is_blacklisted(blocker): - raise ValueError(f"blocker='{blocker}' is blacklisted but function was invoked") + raise RuntimeError(f"blocker='{blocker}' is blacklisted but function was invoked") elif blacklist.is_blacklisted(blocked): - raise ValueError(f"blocked='{blocked}' is blacklisted but function was invoked") + raise RuntimeError(f"blocked='{blocked}' is blacklisted but function was invoked") added = False if not blocks.is_instance_blocked(blocker, blocked, block_level): @@ -137,7 +137,7 @@ def csv_block(blocker: str, url: str, command: str) -> None: elif command == "": raise ValueError("Parameter 'command' is empty") elif blacklist.is_blacklisted(blocker): - raise ValueError(f"blocker='{blocker}' is blacklisted but function was invoked") + raise RuntimeError(f"blocker='{blocker}' is blacklisted but function was invoked") logger.debug("Setting last_blocked for blocker='%s' ...", blocker) instances.set_last_blocked(blocker) diff --git a/fba/http/csrf.py b/fba/http/csrf.py index a9102f1..3ddfe40 100644 --- a/fba/http/csrf.py +++ b/fba/http/csrf.py @@ -37,7 +37,7 @@ def determine(domain: str, headers: dict) -> dict: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(headers, dict): raise TypeError(f"Parameter headers[]='{type(headers)}' is not of type 'dict'") diff --git a/fba/http/federation.py b/fba/http/federation.py index 6349b7f..5fac558 100644 --- a/fba/http/federation.py +++ b/fba/http/federation.py @@ -77,7 +77,7 @@ def fetch_instances(domain: str, origin: str, software: str, command: str, path: elif path is not None and not path.startswith("/"): raise ValueError(f"path='{path}' does not start with a slash") elif _DEPTH > 0 and instances.is_recent(domain, "last_instance_fetch"): - raise ValueError(f"domain='{domain}' has recently been fetched but function was invoked") + raise RuntimeError(f"domain='{domain}' has recently been fetched but function was invoked") elif software is None and not instances.is_recent(domain, "last_instance_fetch"): try: logger.debug("Software for domain='%s',path='%s' is not set, determining ...", domain, path) @@ -231,7 +231,7 @@ def fetch_peers(domain: str, software: str, origin: str) -> list: elif isinstance(software, str) and software == "": raise ValueError("Parameter 'software' is empty") elif software is not None and software_helper.is_relay(software): - raise ValueError(f"domain='{domain}' is of software='{software}' and isn't supported here.") + raise RuntimeError(f"domain='{domain}' is of software='{software}' and isn't supported here.") elif not isinstance(origin, str) and origin is not None: raise TypeError(f"Parameter origin[]='{type(origin)}' is not of type 'str'") elif isinstance(origin, str) and origin == "": @@ -605,7 +605,7 @@ def fetch_blocks(domain: str) -> list: if blacklist.is_blacklisted(domain): raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # Init block list blocklist = [] diff --git a/fba/http/network.py b/fba/http/network.py index 8a3bd67..fe2cacd 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -70,7 +70,7 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = {}) -> domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(path, str): raise TypeError(f"path[]='{type(path)}' is not of type 'str'") elif path == "": @@ -174,7 +174,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(path, str): raise TypeError(f"path[]='{type(path)}' is not of type 'str'") elif path == "": @@ -233,7 +233,7 @@ def send_bot_post(domain: str, blocklist: list) -> None: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(blocklist, list): raise TypeError(f"Parameter blocklist[]='{type(blocklist)}' is not of type 'list'") elif len(blocklist) == 0: @@ -285,7 +285,7 @@ def _fetch_response(domain: str, path: str, headers: dict, timeout: tuple, allow domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(path, str): raise TypeError(f"Parameter path[]='{type(path)}' is not of type 'str'") elif path == "": @@ -429,7 +429,7 @@ def get_generic(domain: str, path: str, allow_redirects: bool = False) -> reques domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(path, str): raise TypeError(f"Parameter path[]='{type(path)}' is not of type 'str'") elif path == "": diff --git a/fba/models/blocks.py b/fba/models/blocks.py index ee71d4c..dd4979a 100644 --- a/fba/models/blocks.py +++ b/fba/models/blocks.py @@ -44,7 +44,7 @@ def get_reason(blocker: str, blocked: str, block_level: str) -> str: elif blacklist.is_blacklisted(blocked): raise RuntimeError(f"blocked='{blocked}' is blacklisted but function invoked") elif not is_instance_blocked(blocker, blocked, block_level): - raise RuntimeError(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function is invoked") + raise RuntimeError(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function was invoked") database.cursor.execute( "SELECT reason FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", @@ -79,7 +79,7 @@ def update_reason(reason: str, blocker: str, blocked: str, block_level: str) -> elif blacklist.is_blacklisted(blocked): raise RuntimeError(f"blocked='{blocked}' is blacklisted but function invoked") elif not is_instance_blocked(blocker, blocked, block_level): - raise RuntimeError(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function is invoked") + raise RuntimeError(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function was invoked") logger.debug("Updating block reason='%s',blocker='%s',blocked='%s',block_level='%s'", reason, blocker, blocked, block_level) database.cursor.execute( @@ -110,7 +110,7 @@ def update_last_seen(blocker: str, blocked: str, block_level: str) -> None: elif blacklist.is_blacklisted(blocked): raise RuntimeError(f"blocked='{blocked}' is blacklisted but function invoked") elif not is_instance_blocked(blocker, blocked, block_level): - raise RuntimeError(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function is invoked") + raise RuntimeError(f"blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' is not registered but function was invoked") database.cursor.execute( "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", diff --git a/fba/models/error_log.py b/fba/models/error_log.py index bbdd94c..718540d 100644 --- a/fba/models/error_log.py +++ b/fba/models/error_log.py @@ -35,7 +35,7 @@ def add(domain: str, error: dict) -> None: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not _write_error_log: raise RuntimeError("Logging of errors is disabled but function was invoked") diff --git a/fba/models/instances.py b/fba/models/instances.py index 61b8e28..f9e23eb 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -109,7 +109,7 @@ def has_pending(domain: str) -> bool: domain_helper.raise_on(domain) if not is_registered(domain): - raise ValueError(f"domain='{domain}' is not registered but function was invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") elif blacklist.is_blacklisted(domain): raise RuntimeError(f"domain='{domain}' is blacklisted but function has been invoked") diff --git a/fba/networks/friendica.py b/fba/networks/friendica.py index 95356d5..e5c94cc 100644 --- a/fba/networks/friendica.py +++ b/fba/networks/friendica.py @@ -35,9 +35,9 @@ def fetch_blocks(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") blocklist = [] block_tag = None diff --git a/fba/networks/lemmy.py b/fba/networks/lemmy.py index 7ecd7d0..6b5d739 100644 --- a/fba/networks/lemmy.py +++ b/fba/networks/lemmy.py @@ -73,9 +73,9 @@ def fetch_peers(domain: str, origin: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # No CSRF by default, you don't have to add network.api_headers by yourself here @@ -133,9 +133,9 @@ def fetch_blocks(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") blocklist = [] @@ -243,7 +243,7 @@ def fetch_instances(domain: str, origin: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") peers = [] diff --git a/fba/networks/mastodon.py b/fba/networks/mastodon.py index e4e6236..c74c289 100644 --- a/fba/networks/mastodon.py +++ b/fba/networks/mastodon.py @@ -71,9 +71,9 @@ def fetch_blocks_from_about(domain: str) -> dict: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # Init variables doc = None @@ -167,9 +167,9 @@ def fetch_blocks(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # Init variables blocklist = [] diff --git a/fba/networks/misskey.py b/fba/networks/misskey.py index 1f54985..d958e83 100644 --- a/fba/networks/misskey.py +++ b/fba/networks/misskey.py @@ -37,9 +37,9 @@ def fetch_peers(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") logger.debug("domain='%s' is misskey, sending API POST request ...", domain) peers = [] @@ -136,9 +136,9 @@ def fetch_blocks(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # No CSRF by default, you don't have to add network.api_headers by yourself here headers = {} diff --git a/fba/networks/peertube.py b/fba/networks/peertube.py index ab1aa59..33f0730 100644 --- a/fba/networks/peertube.py +++ b/fba/networks/peertube.py @@ -33,9 +33,9 @@ def fetch_peers(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # Init variables peers = [] diff --git a/fba/networks/pleroma.py b/fba/networks/pleroma.py index 74fa3b8..fea777b 100644 --- a/fba/networks/pleroma.py +++ b/fba/networks/pleroma.py @@ -56,9 +56,9 @@ def fetch_blocks(domain: str) -> list: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # Init variables blockdict = [] @@ -292,9 +292,9 @@ def fetch_blocks_from_about(domain: str) -> dict: domain_helper.raise_on(domain) if blacklist.is_blacklisted(domain): - raise RuntimeError(f"domain='{domain}' is blacklisted but function is invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not instances.is_registered(domain): - raise RuntimeError(f"domain='{domain}' is not registered but function is invoked") + raise RuntimeError(f"domain='{domain}' is not registered but function was invoked") # Init variables doc = None diff --git a/fba/utils.py b/fba/utils.py index 2ef3de6..fcb09f0 100644 --- a/fba/utils.py +++ b/fba/utils.py @@ -88,7 +88,7 @@ def deobfuscate(domain: str, blocker: str, domain_hash: str = None) -> str: domain_helper.raise_on(blocker) if validators.domain(domain, rfc_2782=True) and blacklist.is_blacklisted(domain): - raise ValueError(f"domain='{domain}' is blacklisted but function was invoked") + raise RuntimeError(f"domain='{domain}' is blacklisted but function was invoked") elif not isinstance(domain_hash, str) and domain_hash is not None: raise TypeError(f"Parameter domain_hash[]='{type(domain_hash)}' is not of type 'str'") elif domain_hash == "":