fix: guard against malformed API response and empty matches list

This commit is contained in:
Stephan Kerkman 2026-05-30 21:01:47 +02:00
parent f308d006e4
commit c75757a70c
2 changed files with 16 additions and 11 deletions

View file

@ -53,14 +53,17 @@ def identify(images):
if not results: if not results:
raise PlantNetError('No match found — try a different photo or organ.') raise PlantNetError('No match found — try a different photo or organ.')
return [ try:
{ return [
'scientific_name': r['species']['scientificNameWithoutAuthor'], {
'common_names': r['species'].get('commonNames', []), 'scientific_name': r['species']['scientificNameWithoutAuthor'],
'score': r['score'], 'common_names': r['species'].get('commonNames', []),
'gbif_id': (r.get('gbif') or {}).get('id'), 'score': r['score'],
'family': r['species'].get('family', {}).get('scientificNameWithoutAuthor', ''), 'gbif_id': (r.get('gbif') or {}).get('id'),
'genus': r['species'].get('genus', {}).get('scientificNameWithoutAuthor', ''), 'family': r['species'].get('family', {}).get('scientificNameWithoutAuthor', ''),
} 'genus': r['species'].get('genus', {}).get('scientificNameWithoutAuthor', ''),
for r in results[:5] }
] for r in results[:5]
]
except (KeyError, TypeError) as exc:
raise PlantNetError('Unexpected API response format.') from exc

View file

@ -63,6 +63,8 @@ def identify_confirm(request):
return redirect('identify_upload') return redirect('identify_upload')
matches = request.session['identify_matches'] matches = request.session['identify_matches']
if not matches:
return redirect('identify_upload')
if request.method == 'POST': if request.method == 'POST':
try: try: