From 03d04fa256075adf862b4223ee198b6597e1e820 Mon Sep 17 00:00:00 2001 From: matograine <matograine@zaclys.net> Date: Sat, 15 Jan 2022 15:15:43 +0100 Subject: [PATCH] [enh] #416 modify wot_lookup error handling - remove wot_lookup error handling - create exit_on_http_error helper function to nicely display error messages - use try/catch statements instead of wot_lookup() --- silkaj/idty_tools.py | 9 ++++++--- silkaj/network_tools.py | 11 +++++++++++ silkaj/wot.py | 16 +++++++++++++--- silkaj/wot_tools.py | 10 ++-------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/silkaj/idty_tools.py b/silkaj/idty_tools.py index e812cea1..92866a34 100644 --- a/silkaj/idty_tools.py +++ b/silkaj/idty_tools.py @@ -15,6 +15,7 @@ import shutil import sys +import urllib import click import pendulum @@ -55,12 +56,14 @@ def check_many_identities(idty, doc_type="Identity"): Returns True if the same identity is found, False if not. """ error_no_identical_id = f"{doc_type} document does not match any valid identity." - # if no id, exit is a bit rough... + try: results_pubkey = wt.wot_lookup(idty.pubkey) results_uid = wt.wot_lookup(idty.uid) - except SystemExit as e: - sys.exit(f"{error_no_identical_id} Error: {e}") + except urllib.error.HTTPError as e: + sys.exit( + f"{error_no_identical_id}\nuid: {idty.uid}\npubkey: {display_pubkey_and_checksum(idty.pubkey)}" + ) # get all matching identities lookup_ids = merge_ids_lists(results_pubkey, results_uid, idty.currency) diff --git a/silkaj/network_tools.py b/silkaj/network_tools.py index db122869..77427a3b 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -91,3 +91,14 @@ def send_document(bma_path, document): print(f"{doc_name} successfully sent") except urllib.error.HTTPError as e: sys.exit(f"Error while publishing {doc_name.lower()}: {e}") + + +def exit_on_http_error(error, error_codes, message): + """ + Nicely displays a message on an expected error code given in a list. + Else, displays the HTTP error message. + """ + for code in error_codes: + if error.code == code: + sys.exit(message) + sys.exit(error) diff --git a/silkaj/wot.py b/silkaj/wot.py index d66bba6b..1d1957ea 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Affero General Public License # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. +import sys +import urllib from collections import OrderedDict import click @@ -25,7 +27,7 @@ from silkaj import wot_tools as wt from silkaj.blockchain_tools import BlockchainParams from silkaj.constants import DATE from silkaj.crypto_tools import is_pubkey_and_check -from silkaj.network_tools import ClientInstance +from silkaj.network_tools import ClientInstance, exit_on_http_error from silkaj.tools import message_exit from silkaj.tui import display_pubkey_and_checksum @@ -145,7 +147,11 @@ def id_pubkey_correspondence(uid_pubkey): if checked_pubkey: uid_pubkey = checked_pubkey - lookups = wt.wot_lookup(uid_pubkey) + try: + lookups = wt.wot_lookup(uid_pubkey) + except urllib.error.HTTPError as e: + exit_on_http_error(e, [404], f"No identity found for {uid_pubkey}") + content = f"Public keys or user id found matching '{uid_pubkey}':\n" for lookup in lookups: for identity in lookup["uids"]: @@ -161,7 +167,11 @@ def choose_identity(pubkey_uid): If there is one uid, returns it If there is multiple uids, prompt a selector """ - lookups = wt.wot_lookup(pubkey_uid) + + try: + lookups = wt.wot_lookup(pubkey_uid) + except urllib.error.HTTPError as e: + exit_on_http_error(e, [404], f"No identity found for {pubkey_uid}") # Generate table containing the choices identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []} diff --git a/silkaj/wot_tools.py b/silkaj/wot_tools.py index 958e6d8b..17a54d56 100644 --- a/silkaj/wot_tools.py +++ b/silkaj/wot_tools.py @@ -14,7 +14,6 @@ # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import sys -import urllib from duniterpy.api.bma import wot from duniterpy.api.errors import DuniterError @@ -53,13 +52,8 @@ def wot_lookup(identifier): if one identity found """ client = ClientInstance().client - try: - results = client(wot.lookup, identifier) - return results["results"] - except DuniterError as e: - sys.exit(e.message) - except urllib.error.HTTPError as e: - sys.exit(e) + results = client(wot.lookup, identifier) + return results["results"] def identities_from_pubkeys(pubkeys, uids): -- GitLab