🐛 Validate pin existence before adding image and improve error handling

master
Alix JEUDI--LEMOINE 2 days ago
parent 1b2df687c5
commit 0b6973c939

@ -131,6 +131,10 @@ async def add_image(
else: else:
os.remove(temp_path) os.remove(temp_path)
if not pin_id == 'null':
if(pins_collection.find_one({"_id": ObjectId(pin_id)}) is None):
raise HTTPException(status_code=404, detail="Pin not found")
# Créer l'entrée en base de données # Créer l'entrée en base de données
image_doc = { image_doc = {
"pin_id": ObjectId(pin_id) if pin_id != 'null' else None, "pin_id": ObjectId(pin_id) if pin_id != 'null' else None,
@ -148,8 +152,8 @@ async def add_image(
return {"id": str(image_id)} return {"id": str(image_id)}
except Exception as e: except (OSError, IOError) as e:
# Nettoyer en cas d'erreur # Nettoyer en cas d'erreur liée au système de fichiers
if os.path.exists(temp_path): if os.path.exists(temp_path):
os.remove(temp_path) os.remove(temp_path)
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))

Loading…
Cancel
Save