1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-09-27 14:43:04 +00:00

handle errors when adding accounts

This commit is contained in:
Lynne Megido 2019-09-10 13:12:40 +10:00
parent c9da5d9f40
commit 43a1ff62b6

View file

@ -209,11 +209,24 @@ def bot_accounts_add():
# look up user # look up user
handle_list = request.form['account'].split('@') handle_list = request.form['account'].split('@')
if len(handle_list) != 3:
# not formatted correctly
error = "Incorrectly formatted handle."
return render_template("bot_accounts_add.html", error = error)
username = handle_list[1] username = handle_list[1]
instance = handle_list[2] instance = handle_list[2]
# gab check # gab check
r = requests.get("https://{}/api/v1/instance".format(instance), timeout=10) try:
r = requests.get("https://{}/api/v1/instance".format(instance), timeout=10)
except requests.exceptions.ConnectionError:
error = "Couldn't connect to {}.".format(instance)
return render_template("bot_accounts_add.html", error = error)
except:
error = "An unknown error occurred."
return render_template("bot_accounts_add.html", error = error)
if r.status_code == 200: if r.status_code == 200:
j = r.json() j = r.json()
if 'is_pro' in j['contact_account']: if 'is_pro' in j['contact_account']:
@ -223,12 +236,26 @@ def bot_accounts_add():
# 1. download host-meta to find webfinger URL # 1. download host-meta to find webfinger URL
r = requests.get("https://{}/.well-known/host-meta".format(instance), timeout=10) r = requests.get("https://{}/.well-known/host-meta".format(instance), timeout=10)
if r.status_code != 200:
error = "Couldn't get host-meta."
return render_template("bot_accounts_add.html", error = error)
# 2. use webfinger to find user's info page # 2. use webfinger to find user's info page
#TODO: use more reliable method #TODO: use more reliable method
uri = re.search(r'template="([^"]+)"', r.text).group(1) try:
uri = uri.format(uri = "{}@{}".format(username, instance)) uri = re.search(r'template="([^"]+)"', r.text).group(1)
uri = uri.format(uri = "{}@{}".format(username, instance))
except:
error = "Couldn't find WebFinger URL."
return render_template("bot_accounts_add.html", error = error)
r = requests.get(uri, headers={"Accept": "application/json"}, timeout=10) r = requests.get(uri, headers={"Accept": "application/json"}, timeout=10)
j = r.json() try:
j = r.json()
except:
error = "Invalid WebFinger response."
return render_template("bot_accounts_add.html", error = error)
found = False found = False
for link in j['links']: for link in j['links']:
if link['rel'] == 'self': if link['rel'] == 'self':