Skip to content

Commit

Permalink
Catch ValueError from prepare_keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
horazont committed Oct 5, 2019
1 parent 0f667d3 commit 2dac54d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions muchopper/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ def search():
search_address = True
search_description = True
search_name = True
invalid_keywords = False

if "q" in request.args:
orig_keywords = request.args["q"]
Expand All @@ -537,7 +538,11 @@ def search():
search_description = "sindescr" in request.args
search_name = "sinname" in request.args

keywords = queries.prepare_keywords(orig_keywords)
try:
keywords = queries.prepare_keywords(orig_keywords)
except ValueError:
keywords = []
invalid_keywords = True

canonical_args = {
"f": "y",
Expand Down Expand Up @@ -596,6 +601,7 @@ def search():
search_name=search_name,
keywords=keywords,
canonical_url=canonical_url,
invalid_keywords=invalid_keywords,
)


Expand Down Expand Up @@ -942,7 +948,15 @@ def api_search():
)

if isinstance(keywords, str):
prepped_keywords = queries.prepare_keywords(keywords)
try:
prepped_keywords = queries.prepare_keywords(keywords)
except ValueError:
return abort_json(
400,
{
"error": "keywords failed to parse"
}
)
elif isinstance(keywords, list):
prepped_keywords = queries.filter_keywords(keywords, min_length=3)
else:
Expand Down
6 changes: 5 additions & 1 deletion muchopper/web/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
</div>
{% endblock %}
{% block content %}
{% if no_keywords %}
{% if invalid_keywords %}
<p class="box error">
<strong>The given keywords are malformed.</strong> Try removing special characters or wrapping them in single or double quotes.
</p>
{% elif no_keywords %}
<p class="box error">
<strong>No search keywords found in input.</strong> Note that words with less than three codepoints are ignored. Spaces at the beginning and end of a word are ignored.
</p>
Expand Down

0 comments on commit 2dac54d

Please sign in to comment.