# ๐ Diagnostics & Error Capture
**๐ ๏ธ Automatic error screenshots and HTML snapshots**
*Debug smarter with visual evidence*
---
## ๐ฏ What is Diagnostics?
The diagnostics system **automatically captures** error screenshots and HTML snapshots when issues occur during script execution, providing visual evidence for troubleshooting.
### **Key Features**
- ๐ธ **Auto-screenshot** โ Visual error capture
- ๐ **HTML snapshots** โ Complete page source
- ๐ฆ **Rate limiting** โ Prevents storage bloat
- ๐๏ธ **Auto-cleanup** โ Configurable retention
- ๐ **Privacy-safe** โ Local storage only
---
## โ๏ธ Configuration
### **Basic Setup**
Add to `src/config.jsonc`:
```json
{
"diagnostics": {
"enabled": true,
"saveScreenshot": true,
"saveHtml": true,
"maxPerRun": 2,
"retentionDays": 7
}
}
```
### **Configuration Options**
| Setting | Default | Description |
|---------|---------|-------------|
| `enabled` | `true` | Master toggle for diagnostics capture |
| `saveScreenshot` | `true` | Capture PNG screenshots on errors |
| `saveHtml` | `true` | Save page HTML content on errors |
| `maxPerRun` | `2` | Maximum captures per script run |
| `retentionDays` | `7` | Auto-delete reports older than N days |
---
## ๐ How It Works
### **Automatic Triggers**
The system captures when these errors occur:
- โฑ๏ธ **Page navigation timeouts**
- ๐ฏ **Element selector failures**
- ๐ **Authentication errors**
- ๐ **Network request failures**
- โก **JavaScript execution errors**
### **Capture Process**
1. **Error Detection** โ Script encounters unhandled error
2. **Visual Capture** โ Screenshot + HTML snapshot
3. **Safe Storage** โ Local `reports/` folder
4. **Continue Execution** โ No blocking or interruption
---
## ๐ File Structure
### **Storage Organization**
```
reports/
โโโ 2025-01-20/
โ โโโ error_abc123_001.png
โ โโโ error_abc123_001.html
โ โโโ error_def456_002.png
โ โโโ error_def456_002.html
โโโ 2025-01-21/
โโโ ...
```
> ๐ Security incidents (login blocks, recovery mismatches) are stored separately under `diagnostics/security-incidents/-slug/` and always include both a screenshot and HTML snapshot for investigation.
### **File Naming Convention**
```
error_[runId]_[sequence].[ext]
```
- **RunId** โ Unique identifier for each script execution
- **Sequence** โ Incremental counter (001, 002, etc.)
- **Extension** โ `.png` for screenshots, `.html` for source
---
## ๐งน Retention Management
### **Automatic Cleanup**
- Runs after each script completion
- Deletes entire date folders older than `retentionDays`
- Prevents unlimited disk usage growth
### **Manual Cleanup**
```powershell
# Remove all diagnostic reports
Remove-Item -Recurse -Force reports/
# Remove reports older than 3 days
Get-ChildItem reports/ | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-3)} | Remove-Item -Recurse -Force
```
---
## ๐ Use Cases
| Scenario | Benefit |
|----------|---------|
| **๐ Development & Debugging** | Visual confirmation of page state during errors |
| **๐ Element Detection Issues** | HTML source analysis for selector problems |
| **๐ Production Monitoring** | Evidence collection for account issues |
| **โก Performance Analysis** | Timeline reconstruction of automation failures |
---
## โก Performance Impact
### **Resource Usage**
- **Screenshots** โ ~100-500KB each
- **HTML files** โ ~50-200KB each
- **CPU overhead** โ Minimal (only during errors)
- **Memory impact** โ Asynchronous, non-blocking
### **Storage Optimization**
- Daily cleanup prevents accumulation
- Rate limiting via `maxPerRun`
- Configurable retention period
---
## ๐๏ธ Environment Settings
### **Development Mode**
```json
{
"diagnostics": {
"enabled": true,
"maxPerRun": 5,
"retentionDays": 14
}
}
```
### **Production Mode**
```json
{
"diagnostics": {
"enabled": true,
"maxPerRun": 2,
"retentionDays": 3
}
}
```
### **Debug Verbose Logging**
```powershell
$env:DEBUG_REWARDS_VERBOSE=1; npm start
```
---
## ๐ ๏ธ Troubleshooting
| Problem | Solution |
|---------|----------|
| **No captures despite errors** | Check `enabled: true`; verify `reports/` write permissions |
| **Excessive storage usage** | Reduce `maxPerRun`; decrease `retentionDays` |
| **Missing screenshots** | Verify browser screenshot API; check memory availability |
| **Cleanup not working** | Ensure script completes successfully for auto-cleanup |
### **Common Capture Locations**
- **Login issues** โ Authentication page screenshots
- **Activity failures** โ Element detection errors
- **Network problems** โ Timeout and connection errors
- **Navigation issues** โ Page load failures
---
## ๐ Integration
### **With Notifications**
Diagnostics complement [Discord Webhooks](./conclusionwebhook.md) and [NTFY](./ntfy.md):
- **Webhooks** โ Immediate error alerts
- **Diagnostics** โ Visual evidence for investigation
- **Combined** โ Complete error visibility
### **With Development Workflow**
```bash
# 1. Run script with diagnostics
npm start
# 2. Check for captures after errors
ls reports/$(date +%Y-%m-%d)/
# 3. Analyze screenshots and HTML
# Open .png files for visual state
# Review .html files for DOM structure
```
---
## ๐ Privacy & Security
- **Local Only** โ All captures stored locally
- **No Uploads** โ Zero external data transmission
- **Account Info** โ May contain sensitive data
- **Secure Storage** โ Use appropriate folder permissions
- **Regular Cleanup** โ Recommended for sensitive environments
---
## ๐ Related Guides
- **[Getting Started](./getting-started.md)** โ Initial setup and configuration
- **[Discord Webhooks](./conclusionwebhook.md)** โ Error notification alerts
- **[NTFY Notifications](./ntfy.md)** โ Mobile push notifications
- **[Security](./security.md)** โ Privacy and data protection