JavaScript Error Monitoring System¶
Automatically detect, log, and alert on JavaScript errors across your entire infrastructure
๐ฏ Overview¶
This system provides two-layer JavaScript error detection:
- Template Validation (Pre-deployment)
- Scans templates for JavaScript syntax errors
- Catches issues before they reach production
-
Runs automatically every 30 minutes
-
Runtime Monitoring (Production)
- Captures real JavaScript errors from user browsers
- Aggregates errors and sends alerts
- Runs automatically every 15 minutes
๐ Quick Start¶
1. Deploy the Monitoring System¶
2. Add Flask Endpoints to Your Apps¶
Add this to story-stages/src/app.py and passgo/src/app.py:
import json
import logging
from datetime import datetime
# Configure JS error logging
js_error_logger = logging.getLogger('js_errors')
js_error_handler = logging.FileHandler('/var/log/js-runtime-errors/{app_name}_errors.log')
js_error_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))
js_error_logger.addHandler(js_error_handler)
js_error_logger.setLevel(logging.INFO)
@app.route('/api/log-js-error', methods=['POST'])
def log_js_error():
"""Endpoint to receive JavaScript errors from frontend"""
try:
data = request.get_json()
errors = data.get('errors', [])
for error in errors:
js_error_logger.error(json.dumps(error))
return jsonify({'status': 'ok', 'received': len(errors)}), 200
except Exception as e:
app.logger.error(f"Failed to log JS error: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
3. Add Error Logger to Your Templates¶
Add this to the <head> section of your base templates:
For story-stages (templates/base.html or dashboard.html):
For passgo:
4. Create Static JS Directory (if needed)¶
ssh [email protected] "mkdir -p /var/www/story-stages/static/js"
ssh [email protected] "mkdir -p /var/www/passgo/static/js"
5. Restart Your Applications¶
ssh [email protected] "sudo systemctl restart story-stages passgo"
๐ Monitoring Features¶
Template Validation¶
- Checks for:
- Unescaped quotes in JavaScript strings
- Mismatched braces in
<script>blocks -
Common syntax errors
-
Schedule: Every 30 minutes
- Alerts: Email when errors detected
Runtime Monitoring¶
- Captures:
- Uncaught exceptions
- Unhandled promise rejections
-
Console errors
-
Threshold: Alerts after 3 errors in 60 minutes
- Schedule: Checks every 15 minutes
๐ ๏ธ Manual Commands¶
Run Validation Manually¶
ssh [email protected] "sudo /usr/local/bin/js-validator.sh"
Check Runtime Errors¶
ssh [email protected] "sudo /usr/local/bin/runtime-error-monitor.sh"
View Validation Logs¶
ssh [email protected] "sudo journalctl -u js-validation.service -f"
View Runtime Monitor Logs¶
ssh [email protected] "sudo journalctl -u js-runtime-monitor.service -f"
Check Error Logs¶
# Story Stages errors
ssh [email protected] "sudo tail -f /var/log/js-runtime-errors/story-stages_errors.log"
# PassGo errors
ssh [email protected] "sudo tail -f /var/log/js-runtime-errors/passgo_errors.log"
# Validation errors
ssh [email protected] "sudo tail -f /var/log/js-validation/errors.log"
Check Timer Status¶
ssh [email protected] "systemctl list-timers | grep -E '(js-validation|js-runtime-monitor)'"
๐ง Email Alerts¶
You'll receive email alerts for: - โ๏ธ Template validation failures (immediate) - โ๏ธ Runtime errors exceeding threshold (max 1/hour)
Cooldown: 1 hour between similar alerts to prevent spam
๐ง Configuration¶
Edit ~/infrastructure/ansible/roles/js_error_monitoring/defaults/main.yml:
# Email settings
owner_email: [email protected]
# Schedules (cron format)
validation_schedule: "*/30 * * * *" # Every 30 minutes
runtime_check_schedule: "*/15 * * * *" # Every 15 minutes
# Thresholds
max_errors_before_alert: 3
error_lookback_minutes: 60
After changes, redeploy:
๐งช Testing¶
Test Template Validation¶
- Introduce a syntax error in a template
- Run:
sudo /usr/local/bin/js-validator.sh - Check for email alert
Test Runtime Monitoring¶
- Add this to a page to trigger an error:
- Visit the page
- Wait 15 minutes or run:
sudo /usr/local/bin/runtime-error-monitor.sh - Check for email alert
๐ Integration with Self-Healing¶
This system integrates with your existing self-healing infrastructure: - Uses same email configuration - Follows ansai workflows (config-as-code) - Systemd-based scheduling - Comprehensive logging
๐ฏ Benefits¶
โ
Proactive Detection - Catch errors before users report them
โ
Automated Alerts - Know immediately when something breaks
โ
Production Monitoring - See real errors from real users
โ
Template Safety - Validate before deployment
โ
Zero Maintenance - Fully automated, runs in background
๐ Troubleshooting¶
Validator not running?¶
ssh [email protected] "systemctl status js-validation.timer"
ssh [email protected] "sudo systemctl start js-validation.timer"
Monitor not running?¶
ssh [email protected] "systemctl status js-runtime-monitor.timer"
ssh [email protected] "sudo systemctl start js-runtime-monitor.timer"
Not receiving emails?¶
- Check SMTP settings in
defaults/main.yml - Test email:
sudo /usr/local/bin/js-validator.sh - Check logs:
sudo journalctl -u js-validation.service
Your JavaScript is now under constant surveillance! ๐๐ก๏ธ