# Import Error Troubleshooting Guide

## 🔴 HTTP 500 Error During Import

This guide helps you diagnose and fix HTTP 500 errors that occur during product import on production.

---

## 📊 Your Current Situation

**Error:** Network error: HTTP error status 500  
**Progress:** 100/697 products (14% complete)  
**Status:** Import failed with 597 products remaining  

---

## 🔍 Step 1: Check Production Logs

### **A. Check Laravel Error Log**

**Location:** `storage/logs/laravel.log`

**How to access on GoDaddy:**
1. Log into cPanel
2. Go to File Manager
3. Navigate to: `public_html/Code/html2/storage/logs/`
4. Download or view `laravel.log`
5. Look for the most recent error (around the time of import failure)

**What to look for:**
```
[2025-11-18 16:xx:xx] production.ERROR: === BATCH IMPORT FAILED ===
{
    "error_message": "...",
    "processed_so_far": 100,
    "memory_usage": "128MB",
    "memory_peak": "256MB"
}
```

---

### **B. Check Performance Log**

**Location:** `storage/logs/import-performance.log`

**What to look for:**
```
[2025-11-18] local.INFO: === BATCH IMPORT STARTED === {"total_products":30}
[2025-11-18] local.ERROR: Product import failed {"sku":"PRODUCT-123","error":"..."}
[2025-11-18] local.ERROR: === BATCH IMPORT FAILED === {"processed_so_far":100}
```

---

## 🎯 Common Causes & Solutions

### **1. Memory Exhaustion** (Most Common)

**Symptoms:**
```
Fatal error: Allowed memory size of 134217728 bytes exhausted
```

**Why it happens:**
- Batch size too large (currently 30 products)
- Large images consuming memory
- 697 products is a large import

**Solution A: Reduce Batch Size**
Edit `SimplifiedImportController.php` line 178:
```php
// Change from:
$batchSize = 30;

// To:
$batchSize = 20; // Or even 15 for very large imports
```

**Solution B: Increase PHP Memory Limit**
Edit `.htaccess` in your root directory:
```apache
php_value memory_limit 256M
```

Or contact GoDaddy support to increase memory limit.

---

### **2. PHP Timeout**

**Symptoms:**
```
Maximum execution time of 30 seconds exceeded
```

**Why it happens:**
- Batch processing taking too long
- Slow image downloads
- Database operations timing out

**Solution: Increase PHP Timeout**
Edit `.htaccess`:
```apache
php_value max_execution_time 300
```

---

### **3. Database Connection Lost**

**Symptoms:**
```
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
```

**Why it happens:**
- Database connection timeout
- Too many queries
- Database server overload

**Solution A: Reduce Batch Size**
```php
$batchSize = 15; // Smaller batches = fewer queries per request
```

**Solution B: Check Database Connection**
- Verify database is accessible
- Check for database server errors in cPanel

---

### **4. Image Download Failures**

**Symptoms:**
```
cURL error 28: Operation timed out
Image download failed
```

**Why it happens:**
- Invalid image URLs
- Slow image servers
- Network issues

**Solution: Check Performance Log**
Look for:
```
[2025-11-18] local.WARNING: Image download failed {"url":"...","error":"..."}
```

**Fix:**
- Verify image URLs are accessible
- Remove or fix problematic image URLs in Excel file
- Consider skipping images temporarily to test

---

### **5. Invalid Product Data**

**Symptoms:**
```
Product creation returned null
Validation failed
```

**Why it happens:**
- Missing required fields
- Invalid SKU format
- Duplicate SKUs

**Solution: Check Performance Log**
Look for:
```
[2025-11-18] local.ERROR: Product import failed {"sku":"PRODUCT-123","error":"..."}
```

**Fix:**
- Verify the SKU that failed
- Check Excel file for that product
- Fix data and re-import

---

## 🛠️ Immediate Actions

### **Step 1: Get the Exact Error**

Run this command on your server (via SSH or cPanel Terminal):
```bash
tail -100 storage/logs/laravel.log
```

Or download the file and search for the most recent error.

### **Step 2: Identify the Problem**

Match the error message with the causes above:
- Memory error → Reduce batch size or increase memory
- Timeout error → Increase timeout or reduce batch size
- Database error → Check database connection
- Image error → Check image URLs
- Product error → Check product data

### **Step 3: Apply the Fix**

Based on the error, apply the appropriate solution from above.

### **Step 4: Re-test**

1. Upload the same Excel file
2. Start import again
3. Monitor progress
4. Check logs if it fails again

---

## 📋 Quick Fixes to Try First

### **Fix 1: Reduce Batch Size to 20**

**File:** `SimplifiedImportController.php` (line 178)
```php
$batchSize = 20; // Reduced from 30
```

**Why:** Smaller batches use less memory and are more stable

---

### **Fix 2: Increase Memory Limit**

**File:** `.htaccess` (in root directory)
```apache
php_value memory_limit 256M
php_value max_execution_time 300
```

**Why:** Gives more resources for large imports

---

### **Fix 3: Test with Smaller File**

1. Create a test Excel file with only 50 products
2. Try importing it
3. If it works, the issue is with large file size
4. Solution: Import in smaller chunks (100-200 products at a time)

---

## 🔧 Advanced Diagnostics

### **Check Memory Usage**

The enhanced error logging now shows:
```json
{
    "memory_usage": "128MB",
    "memory_peak": "256MB"
}
```

**If memory_peak is close to your limit:**
- Reduce batch size
- Increase memory limit
- Clear cache before importing

### **Check Which Product Failed**

Look in performance log for:
```
[2025-11-18] local.ERROR: Product import failed {
    "sku": "PRODUCT-123",
    "error": "...",
    "time_spent": "5000ms"
}
```

**This tells you:**
- Which product caused the failure
- What the error was
- How long it took before failing

### **Check Batch Progress**

Look for:
```
[2025-11-18] local.INFO: Processing batch {"processed":90,"total":697}
[2025-11-18] local.INFO: Processing batch {"processed":120,"total":697}
```

**If it stops between batches:**
- The error happened during batch processing
- Check the batch that was being processed (products 91-120)

---

## 📞 Getting Help

### **Information to Provide:**

When asking for help, provide:

1. **Error message** from `laravel.log`
2. **Last entries** from `import-performance.log`
3. **Memory usage** from error details
4. **Which product/batch** failed (if shown in logs)
5. **Server specifications** (PHP version, memory limit, etc.)

### **How to Get Server Info:**

Create a file `info.php` in your root directory:
```php
<?php
phpinfo();
?>
```

Visit: `https://yoursite.com/info.php`

Look for:
- **memory_limit** (should be at least 128M)
- **max_execution_time** (should be at least 120)
- **post_max_size** (should be at least 20M)
- **upload_max_filesize** (should be at least 20M)

**Delete this file after checking!**

---

## ✅ Recommended Configuration for Large Imports

### **SimplifiedImportController.php**
```php
$batchSize = 20; // Safe for most shared hosting
```

### **.htaccess**
```apache
php_value memory_limit 256M
php_value max_execution_time 300
php_value post_max_size 50M
php_value upload_max_filesize 50M
```

### **Import Strategy**
- For 697 products: Import in 2-3 separate files (200-300 products each)
- Or use batch size of 15-20 and be patient
- Monitor logs during import

---

## 🎯 Next Steps

1. ✅ **Check logs** - Get the exact error message
2. ✅ **Identify cause** - Match error with causes above
3. ✅ **Apply fix** - Implement the appropriate solution
4. ✅ **Test** - Try importing again
5. ✅ **Monitor** - Watch logs for any issues

---

## 📝 Prevention Tips

### **Before Large Imports:**

1. **Test with small file** (10-20 products)
2. **Check server resources** (memory, CPU)
3. **Verify image URLs** (make sure they're accessible)
4. **Clean data** (remove duplicates, fix formatting)
5. **Clear cache** (run `php artisan cache:clear`)

### **During Import:**

1. **Don't close browser** (keep tab open)
2. **Don't start multiple imports** (one at a time)
3. **Monitor progress** (watch the percentage)
4. **Check logs if it fails** (immediate diagnosis)

### **After Import:**

1. **Verify products** (check if they were created)
2. **Check images** (verify they uploaded correctly)
3. **Review logs** (look for warnings or errors)
4. **Clear cache** (refresh product data)

---

## 🚨 Emergency Recovery

### **If Import Gets Stuck:**

1. **Clear session:**
   ```php
   // In Laravel Tinker or temporary route
   session()->forget('import_data');
   ```

2. **Clear cache:**
   ```bash
   php artisan cache:clear
   php artisan config:clear
   ```

3. **Check database:**
   - See how many products were actually created
   - Compare with Excel file
   - Import remaining products

### **If Products Are Duplicated:**

1. **Check by SKU:**
   ```sql
   SELECT sku, COUNT(*) as count 
   FROM products 
   GROUP BY sku 
   HAVING count > 1;
   ```

2. **Delete duplicates** (keep the latest)

---

**Last Updated:** 2025-11-18  
**Version:** 1.0  
**Status:** Production Troubleshooting Guide
