fix: catch all exceptions in identify_upload, log unexpected errors

This commit is contained in:
Stephan Kerkman 2026-05-30 21:59:37 +02:00
parent 6e2f477c09
commit bee9fe0cd8

View file

@ -1,3 +1,4 @@
import logging
import uuid
from django.shortcuts import render, redirect
from django.core.files.base import ContentFile
@ -7,6 +8,8 @@ from plants.services.plantnet import PlantNetError
from plants.services.vpc import search_species as vpc_search
from plants.models import Plant, Species
logger = logging.getLogger(__name__)
_SESSION_KEYS = [
'identify_matches', 'identify_image_paths', 'identify_selected',
'identify_vpc_results', 'identify_vpc_idx',
@ -45,13 +48,23 @@ def identify_upload(request):
saved_paths.append({'path': name, 'organ': organ})
images.append((file_bytes, organ))
error_msg = None
try:
matches = plantnet.identify(images)
except PlantNetError as e:
error_msg = str(e)
except Exception as e:
logger.exception('identify_upload unexpected error: %s', e)
error_msg = 'Identification failed — please try again.'
if error_msg:
for p in saved_paths:
try:
if default_storage.exists(p['path']):
default_storage.delete(p['path'])
return render(request, 'plants/identify_upload.html', {'error': str(e)})
except Exception:
pass
return render(request, 'plants/identify_upload.html', {'error': error_msg})
request.session['identify_matches'] = matches
request.session['identify_image_paths'] = saved_paths