[mod] presearch: hardening engine's response against KeyErrors

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2024-01-13 14:02:13 +01:00 committed by Markus Heiser
parent 0a78f59aba
commit a2c269bbac
1 changed files with 10 additions and 8 deletions

View File

@ -113,7 +113,9 @@ def parse_search_query(json_results):
value = _strip_leading_strings(value) value = _strip_leading_strings(value)
attributes.append({'label': label, 'value': value}) attributes.append({'label': label, 'value': value})
content = [] content = []
for item in [info['subtitle'], info['description']]: for item in [info.get('subtitle'), info.get('description')]:
if not item:
continue
item = _strip_leading_strings(html_to_text(item)) item = _strip_leading_strings(html_to_text(item))
if item: if item:
content.append(item) content.append(item)
@ -135,7 +137,7 @@ def response(resp):
json_resp = resp.json() json_resp = resp.json()
if search_type == 'search': if search_type == 'search':
results = parse_search_query(json_resp['results']) results = parse_search_query(json_resp.get('results'))
elif search_type == 'images': elif search_type == 'images':
for item in json_resp.get('images', []): for item in json_resp.get('images', []):
@ -143,9 +145,9 @@ def response(resp):
{ {
'template': 'images.html', 'template': 'images.html',
'title': item['title'], 'title': item['title'],
'url': item['link'], 'url': item.get('link'),
'img_src': item['image'], 'img_src': item.get('image'),
'thumbnail_src': item['thumbnail'], 'thumbnail_src': item.get('thumbnail'),
} }
) )
@ -158,7 +160,7 @@ def response(resp):
results.append( results.append(
{ {
'title': item['title'], 'title': item['title'],
'url': item['link'], 'url': item.get('link'),
'content': '', 'content': '',
'metadata': ' / '.join(metadata), 'metadata': ' / '.join(metadata),
'img_src': item.get('image'), 'img_src': item.get('image'),
@ -171,8 +173,8 @@ def response(resp):
results.append( results.append(
{ {
'title': item['title'], 'title': item['title'],
'url': item['link'], 'url': item.get('link'),
'content': item['description'], 'content': item.get('description', ''),
'metadata': ' / '.join(metadata), 'metadata': ' / '.join(metadata),
'img_src': item.get('image'), 'img_src': item.get('image'),
} }