From bee9fe0cd8c6717d7123f554336c7f49ec9a1678 Mon Sep 17 00:00:00 2001 From: Stephan Kerkman Date: Sat, 30 May 2026 21:59:37 +0200 Subject: [PATCH] fix: catch all exceptions in identify_upload, log unexpected errors --- plants/views/identify.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plants/views/identify.py b/plants/views/identify.py index 55329da..1403eec 100644 --- a/plants/views/identify.py +++ b/plants/views/identify.py @@ -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: - if default_storage.exists(p['path']): - default_storage.delete(p['path']) - return render(request, 'plants/identify_upload.html', {'error': str(e)}) + try: + if default_storage.exists(p['path']): + default_storage.delete(p['path']) + 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