Error hanlding for user search

This commit is contained in:
Mouse Reeve 2020-09-30 11:23:23 -07:00
parent 9efe1a3990
commit 8dba4ccf47
3 changed files with 11 additions and 5 deletions

View file

@ -43,7 +43,10 @@ def handle_account_search(query):
except models.User.DoesNotExist: except models.User.DoesNotExist:
url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \ url = 'https://%s/.well-known/webfinger?resource=acct:%s' % \
(domain, query) (domain, query)
response = requests.get(url) try:
response = requests.get(url)
except requests.exceptions.ConnectionError:
return None
if not response.ok: if not response.ok:
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
@ -52,8 +55,8 @@ def handle_account_search(query):
try: try:
user = get_or_create_remote_user(link['href']) user = get_or_create_remote_user(link['href'])
except KeyError: except KeyError:
return HttpResponseNotFound() return None
return user return [user]
def handle_follow(user, to_follow): def handle_follow(user, to_follow):

View file

@ -1,6 +1,9 @@
{% extends 'layout.html' %} {% extends 'layout.html' %}
{% block content %} {% block content %}
<div class="content-container"> <div class="content-container">
{% if not results %}
<p>No results found for "{{ query }}"</p>
{% endif %}
{% for result in results %} {% for result in results %}
<div> <div>
<h2>{{ result.username }}</h2> <h2>{{ result.username }}</h2>

View file

@ -120,9 +120,9 @@ def search(request):
query = request.GET.get('q') query = request.GET.get('q')
if re.match(r'\w+@\w+.\w+', query): if re.match(r'\w+@\w+.\w+', query):
# if something looks like a username, search with webfinger # if something looks like a username, search with webfinger
results = [outgoing.handle_account_search(query)] results = outgoing.handle_account_search(query)
return TemplateResponse( return TemplateResponse(
request, 'user_results.html', {'results': results} request, 'user_results.html', {'results': results, 'query': query}
) )
# or just send the question over to book search # or just send the question over to book search