diff --git a/server/routes/detectors.js b/server/routes/detectors.js index bac32f1..b411444 100644 --- a/server/routes/detectors.js +++ b/server/routes/detectors.js @@ -315,7 +315,59 @@ async function handleDetection(req, res) { } } - const detection = await DroneDetection.create(detectionRecord); + // Create detection record with proper error handling + let detection; + try { + detection = await DroneDetection.create(detectionRecord); + console.log(`✅ Detection created successfully: ID ${detection.id}, Device ${deviceIdString}, Drone ${detectionData.drone_id}`); + } catch (error) { + console.error(`❌ Failed to create detection for device ${deviceIdString}, drone ${detectionData.drone_id}:`, error.message); + + // Log to admin/management for monitoring + console.error(`🚨 ADMIN ALERT: Database error in detection creation - Device: ${deviceIdString}, DroneID: ${detectionData.drone_id}, Error: ${error.message}`); + + // Check for specific database constraint errors + if (error.name === 'SequelizeValidationError') { + return res.status(400).json({ + success: false, + error: 'Validation error', + message: 'Detection data validation failed', + details: error.errors?.map(e => e.message) || ['Invalid data format'], + device_id: deviceIdString, + drone_id: detectionData.drone_id + }); + } + + if (error.name === 'SequelizeUniqueConstraintError') { + return res.status(409).json({ + success: false, + error: 'Duplicate detection', + message: 'Detection with this combination already exists', + device_id: deviceIdString, + drone_id: detectionData.drone_id + }); + } + + if (error.name === 'SequelizeForeignKeyConstraintError') { + return res.status(400).json({ + success: false, + error: 'Reference error', + message: 'Invalid reference to related data', + device_id: deviceIdString, + drone_id: detectionData.drone_id + }); + } + + // Generic database error + return res.status(500).json({ + success: false, + error: 'Database error', + message: 'Failed to store detection data', + device_id: deviceIdString, + drone_id: detectionData.drone_id, + logged_for_admin_review: true + }); + } // Process detection through tracking service for movement analysis (from original) const movementAnalysis = droneTracker.processDetection({ diff --git a/test_enhanced_detection.py b/test_enhanced_detection.py index 2800516..7f4c5d4 100644 --- a/test_enhanced_detection.py +++ b/test_enhanced_detection.py @@ -108,12 +108,16 @@ def send_detection(drone_type=2, drone_id=None, geo_lat=0, geo_lon=0, rssi=-45, ) if show_response: - if response.status_code == 200: - print(f"✅ Detection sent successfully") + if response.status_code in [200, 201]: + print(f"✅ Detection sent successfully (Status: {response.status_code})") try: data = response.json() if data.get('success'): print(f" Response: {data.get('message', 'OK')}") + if 'data' in data and 'id' in data['data']: + print(f" Detection ID: {data['data']['id']}") + if 'data' in data and 'threat_level' in data['data']: + print(f" Threat Level: {data['data']['threat_level']}") else: print(f" Response: {data}") except: @@ -122,7 +126,7 @@ def send_detection(drone_type=2, drone_id=None, geo_lat=0, geo_lon=0, rssi=-45, print(f"❌ Detection failed: {response.status_code}") print(f" Response: {response.text}") - return response.status_code == 200 + return response.status_code in [200, 201] except requests.exceptions.RequestException as e: if show_response: