Skip to content
Snippets Groups Projects
Commit 9fd13edb authored by matograine's avatar matograine Committed by Moul
Browse files

[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()
parent 80714fc0
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import shutil import shutil
import sys import sys
import urllib
import click import click
import pendulum import pendulum
...@@ -55,12 +56,14 @@ def check_many_identities(idty: Identity, doc_type: str = "Identity"): ...@@ -55,12 +56,14 @@ def check_many_identities(idty: Identity, doc_type: str = "Identity"):
Returns True if the same identity is found, False if not. 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." error_no_identical_id = f"{doc_type} document does not match any valid identity."
# if no id, exit is a bit rough...
try: try:
results_pubkey = wt.wot_lookup(idty.pubkey) results_pubkey = wt.wot_lookup(idty.pubkey)
results_uid = wt.wot_lookup(idty.uid) results_uid = wt.wot_lookup(idty.uid)
except SystemExit as e: except urllib.error.HTTPError as e:
sys.exit(f"{error_no_identical_id} Error: {e}") sys.exit(
f"{error_no_identical_id}\nuid: {idty.uid}\npubkey: {display_pubkey_and_checksum(idty.pubkey)}"
)
# get all matching identities # get all matching identities
lookup_ids = merge_ids_lists(results_pubkey, results_uid, idty.currency) lookup_ids = merge_ids_lists(results_pubkey, results_uid, idty.currency)
......
...@@ -91,3 +91,13 @@ def send_document(bma_path, document): ...@@ -91,3 +91,13 @@ def send_document(bma_path, document):
print(f"{doc_name} successfully sent") print(f"{doc_name} successfully sent")
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
sys.exit(f"Error while publishing {doc_name.lower()}: {e}") sys.exit(f"Error while publishing {doc_name.lower()}: {e}")
def exit_on_http_error(error: urllib.error.HTTPError, err_code: int, message: str):
"""
Nicely displays a message on an expected error code.
Else, displays the HTTP error message.
"""
if error.code == err_code:
sys.exit(message)
sys.exit(error)
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import urllib
from collections import OrderedDict from collections import OrderedDict
import click import click
...@@ -25,7 +26,7 @@ from silkaj import wot_tools as wt ...@@ -25,7 +26,7 @@ from silkaj import wot_tools as wt
from silkaj.blockchain_tools import BlockchainParams from silkaj.blockchain_tools import BlockchainParams
from silkaj.constants import DATE from silkaj.constants import DATE
from silkaj.crypto_tools import is_pubkey_and_check 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.tools import message_exit
from silkaj.tui import display_pubkey_and_checksum from silkaj.tui import display_pubkey_and_checksum
...@@ -145,7 +146,11 @@ def id_pubkey_correspondence(uid_pubkey): ...@@ -145,7 +146,11 @@ def id_pubkey_correspondence(uid_pubkey):
if checked_pubkey: if checked_pubkey:
uid_pubkey = 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" content = f"Public keys or user id found matching '{uid_pubkey}':\n"
for lookup in lookups: for lookup in lookups:
for identity in lookup["uids"]: for identity in lookup["uids"]:
...@@ -161,7 +166,11 @@ def choose_identity(pubkey_uid): ...@@ -161,7 +166,11 @@ def choose_identity(pubkey_uid):
If there is one uid, returns it If there is one uid, returns it
If there is multiple uids, prompt a selector 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 # Generate table containing the choices
identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []} identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []}
......
...@@ -14,10 +14,8 @@ ...@@ -14,10 +14,8 @@
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import sys import sys
import urllib
from duniterpy.api.bma import wot from duniterpy.api.bma import wot
from duniterpy.api.errors import DuniterError
from silkaj.network_tools import ClientInstance from silkaj.network_tools import ClientInstance
...@@ -53,13 +51,7 @@ def wot_lookup(identifier): ...@@ -53,13 +51,7 @@ def wot_lookup(identifier):
if one identity found if one identity found
""" """
client = ClientInstance().client client = ClientInstance().client
try: return (client(wot.lookup, identifier))["results"]
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)
def identities_from_pubkeys(pubkeys, uids): def identities_from_pubkeys(pubkeys, uids):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment