Troubleshooting & Debugging
This comprehensive guide provides solutions to common issues and debugging techniques for SparkyFitness developers.
Common Issues and Solutions
Port Conflicts
If you encounter errors indicating that a port is already in use, another application is likely using one of the ports required by SparkyFitness (e.g., 8080 for frontend, 3010 for backend, 5432 for PostgreSQL).
Solution:
- Identify the conflicting process:
- On Linux/macOS:
lsof -i :<port_number>(e.g.,lsof -i :8080) - On Windows:
netstat -ano | findstr :<port_number>followed bytaskkill /PID <PID> /F
- On Linux/macOS:
- Stop the conflicting service or change the port in your
.envfile.
Database Connection Issues
Problems connecting to the PostgreSQL database can arise from incorrect credentials, the database not running, or network issues.
Solution:
- Check database logs:bash
./docker/docker-helper.sh dev logs sparkyfitness-db - Verify
.envsettings: EnsureSPARKY_FITNESS_DB_NAME,SPARKY_FITNESS_DB_USER, andSPARKY_FITNESS_DB_PASSWORDare correct. - Reset database (Development only!): If data integrity is not critical, you can perform a destructive reset.bash
./docker/docker-helper.sh dev down ./docker/docker-helper.sh dev clean ./docker/docker-helper.sh dev up
Build Failures
Issues during the build process (e.g., npm install or Docker image builds) can be caused by corrupted caches or dependency problems.
Solution:
- Clean rebuild:bash
./docker/docker-helper.sh dev build --no-cache - Full reset:bash
./docker/docker-helper.sh dev clean ./docker/docker-helper.sh dev up
Permission Issues (Linux/WSL)
On Linux or Windows Subsystem for Linux (WSL), you might encounter permission errors related to Docker volumes, especially for the PostgreSQL data directory.
Solution:
- Fix volume permissions:bash
sudo chown -R $USER:$USER ./postgresql
"Invalid key length" error
This error typically indicates that your SPARKY_FITNESS_API_ENCRYPTION_KEY in your .env file is not correctly configured. Ensure it is a 64-character hexadecimal string.
Solution: Generate a valid key using:
openssl rand -hex 32
# or
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"Rate Limiting Issues
SparkyFitness implements rate limiting at the Nginx reverse proxy layer to prevent brute-force attacks and DoS attempts.
Common Rate Limiting Errors:
429 Too Many Requestson login/signup endpoints- Connection refused during high traffic
Solution:
- Check rate limit configuration in
docker/nginx.conf.template:nginxlimit_req_zone $binary_remote_addr zone=login_signup_zone:10m rate=5r/s; - Temporary bypass for development:bash
# Edit nginx.conf.template and increase rate limits # Then restart containers ./docker/docker-helper.sh dev restart - Production tuning: Adjust rate limits based on actual usage patterns
Authentication & JWT Issues
Common symptoms:
401 Unauthorizederrors- Token expired messages
- Login session not persisting
Solution:
- Check JWT secret configuration:bash
# Ensure JWT_SECRET is properly set in .env grep JWT_SECRET .env - Clear browser storage:javascript
// In browser console localStorage.clear(); sessionStorage.clear(); - Verify token expiration settings in backend configuration
Debugging Techniques
Application Logs
View all service logs:
./docker/docker-helper.sh dev logsFollow specific service logs:
./docker/docker-helper.sh dev logs -f sparkyfitness-frontend
./docker/docker-helper.sh dev logs -f sparkyfitness-server
./docker/docker-helper.sh dev logs -f sparkyfitness-dbBackend debugging logs:
# Enable debug logging in .env
LOG_LEVEL=debug
# View structured logs
docker exec -it sparkyfitness-server-1 tail -f /app/logs/app.logContainer Inspection
List running containers:
./docker/docker-helper.sh dev psExecute commands in containers:
# Frontend container
docker exec -it sparkyfitness-frontend-1 sh
# Backend container
docker exec -it sparkyfitness-server-1 sh
# Database container
docker exec -it sparkyfitness-db-1 psql -U sparky -d sparkyfitness_dbInspect container resources:
# Check resource usage
docker stats
# Inspect container configuration
docker inspect sparkyfitness-server-1Database Debugging
Connect to PostgreSQL:
docker exec -it sparkyfitness-db-1 psql -U sparky -d sparkyfitness_dbCommon database queries:
-- Check table structure
\dt
-- View recent logs
SELECT * FROM logs ORDER BY created_at DESC LIMIT 10;
-- Check user data
SELECT id, email, created_at FROM users LIMIT 5;
-- Monitor active connections
SELECT * FROM pg_stat_activity;Database migration issues:
# Check migration status
docker exec -it sparkyfitness-server-1 npm run migrate:status
# Force migration reset (DANGEROUS)
docker exec -it sparkyfitness-server-1 npm run migrate:resetNetwork Debugging
Check container networking:
# List Docker networks
docker network ls
# Inspect network configuration
docker network inspect <network_name>
# Test connectivity between containers
docker exec -it sparkyfitness-frontend-1 ping sparkyfitness-serverAPI endpoint testing:
# Test backend health endpoint
curl http://localhost:3010/api/health
# Test with authentication
curl -H "Authorization: Bearer <token>" http://localhost:3010/api/users/profilePerformance Debugging
Monitor resource usage:
# Real-time resource monitoring
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
# System resource usage
htop # or top on basic systemsDatabase performance:
-- Check slow queries
SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
ORDER BY mean_time DESC
LIMIT 10;
-- Check locks
SELECT * FROM pg_locks WHERE NOT GRANTED;Development Environment Issues
Hot Reload Not Working
Frontend hot reload issues:
# Check Vite configuration
cat vite.config.ts
# Restart with clean cache
./docker/docker-helper.sh dev down
./docker/docker-helper.sh dev up --buildEnvironment Variable Issues
Debug environment variables:
# Check .env file is loaded
docker exec -it sparkyfitness-server-1 env | grep SPARKY
# Verify frontend environment variables
docker exec -it sparkyfitness-frontend-1 env | grep VITETypeScript Compilation Issues
Common TypeScript errors:
# Check TypeScript configuration
cat tsconfig.json
# Run type checking manually
docker exec -it sparkyfitness-frontend-1 npm run type-check
# Clear TypeScript cache
rm -rf node_modules/.cacheAdvanced Debugging
Memory Leaks
Identify memory leaks:
# Monitor memory usage over time
watch -n 1 'docker stats --no-stream'
# Use Node.js heap dumps
docker exec -it sparkyfitness-server-1 node --inspect=0.0.0.0:9229 server.jsPerformance Profiling
Backend profiling:
// Add to backend code for profiling
const profiler = require('v8-profiler-next');
// Start profiling
profiler.startProfiling('API_PROFILE');
// End profiling and save
const profile = profiler.stopProfiling('API_PROFILE');
profile.export().pipe(fs.createWriteStream('profile.cpuprofile'));Security Debugging
Check for security vulnerabilities:
# NPM audit
npm audit
# Docker security scanning
docker scan sparkyfitness-server:latestGetting Help
When troubleshooting fails, reach out to the community:
- Discord Community: https://discord.gg/vcnMT5cPEA
- GitHub Discussions: Post detailed questions with logs
- Documentation: Search this comprehensive documentation site
- Issue Templates: Use provided GitHub issue templates for bug reports
Creating Effective Bug Reports
Include the following information:
- Environment details: OS, Docker version, Node.js version
- Steps to reproduce: Clear, numbered steps
- Expected vs. actual behavior: What should happen vs. what happens
- Logs: Relevant log excerpts (use code blocks)
- Configuration: Relevant parts of .env or config files (sanitized)
- Screenshots: For UI issues
Log collection command:
# Collect all relevant logs
./docker/docker-helper.sh dev logs > debug-logs-$(date +%Y%m%d-%H%M%S).txt