Debugging Guide
Techniques and tools for debugging issues in the Jose Madrid Salsa platform
Debugging Guide
This guide covers debugging techniques for common issues in the Jose Madrid Salsa platform, from database connectivity to payment processing.
Server-Side Logging
The platform uses namespaced console logging throughout. Look for these prefixes:
| Prefix | Module |
|---|---|
[Prisma] | Database client initialization |
[Auth] | NextAuth.js authentication |
[RBAC] | Permission checks |
[Tax Calculator] | Stripe Tax calculations |
[Shipping Calculator] | Shipping rate lookups |
[Orders API] | Order creation and queries |
[NextAuth Error] | Authentication errors |
Database Debugging
Connection Issues
Run the built-in diagnostic script:
node scripts/diagnose-db-connection.jsThis checks:
- Environment variable presence
- URL format validity
- Actual database connectivity
Prisma Studio
Open the database GUI for direct inspection:
npm run db:studioThis opens a browser-based interface at http://localhost:5555 where you can browse and edit records.
Query Logging
In development, Prisma logs all queries. Check your terminal for SQL output:
// In lib/prisma.ts - dev mode enables query logging
const logLevels = process.env.NODE_ENV === 'development'
? ['query', 'error', 'warn']
: ['error']Disable Query Logs
If query logs are too noisy during development, temporarily change logLevels to ['error'] in lib/prisma.ts.
Authentication Debugging
NextAuth.js has built-in debug mode enabled in development:
// In lib/auth.ts
debug: process.env.NODE_ENV === 'development',
logger: {
error(code, metadata) {
console.error('[NextAuth Error]', code, metadata)
},
warn(code) {
console.warn('[NextAuth Warn]', code)
},
}Common Auth Issues
If NEXTAUTH_SECRET is missing, auth will fail silently in production. The app validates this at startup:
if (!process.env.NEXTAUTH_SECRET) {
console.error('[Auth] CRITICAL: NEXTAUTH_SECRET is not set')
}Generate one with: openssl rand -base64 32
If getCurrentUser() returns null:
- Check browser cookies for
next-auth.session-token - Verify
NEXTAUTH_URLmatches your actual URL - Check JWT expiration (30 days default)
- Look for
[NextAuth Error]in server logs
CSRF errors usually indicate mismatched NEXTAUTH_URL:
# Must match the URL you access the app from
NEXTAUTH_URL="http://localhost:3000"For production behind a proxy, ensure useSecureCookies is set correctly.
Payment Debugging
Stripe Issues
Check the Stripe Dashboard logs at dashboard.stripe.com/logs for API call details.
Common issues:
- PaymentIntent creation fails: Check
STRIPE_SECRET_KEYis valid - Webhook signature invalid: Verify
STRIPE_WEBHOOK_SECRETmatches your endpoint - Tax calculation returns 0: Run
validateTaxConfiguration()to test Stripe Tax setup
Testing Payments Locally
Use Stripe CLI to forward webhooks:
stripe listen --forward-to localhost:3000/api/webhooks/stripeEmail Debugging
If emails are not sending:
- Check
RESEND_API_KEYis set (missing key = silent skip with warning) - Check server logs for
Failed to send emailmessages - Check Resend dashboard at
resend.com/emailsfor delivery status - Verify
FROM_EMAILmatches a verified domain in Resend
// All email functions check for the API key first
if (!resendApiKey) {
console.warn('RESEND_API_KEY not set; skipping email send')
return { skipped: true }
}Permission Debugging
The RBAC system logs fallback warnings when permission tables are missing:
[RBAC] No persisted permission "orders:read" for role STAFF.
Falling back to default map.This is expected on first setup before running tsx prisma/seed.permissions.ts.
To debug permission issues:
import { getUserPermissions } from '@/lib/rbac'
const user = await getCurrentUser()
const permissions = await getUserPermissions(user)
console.log('User permissions:', permissions)API Route Debugging
Rate Limiting
If you get 429 responses, check the rate limit configuration:
// lib/rate-limiter.ts
API_GENERAL: { maxRequests: 100, windowSeconds: 60 }
AUTH_LOGIN: { maxRequests: 5, windowSeconds: 900 }
PASSWORD_RESET:{ maxRequests: 3, windowSeconds: 3600 }Rate limit headers are included in responses:
X-RateLimit-Limit- Current countX-RateLimit-Remaining- Requests leftX-RateLimit-Reset- Seconds until reset
Debugging with Node Inspector
npm run dev:debugThen open chrome://inspect in Chrome to connect to the Node.js debugger.
Shipping Debugging
The shipping calculator logs detailed error information:
[Shipping Calculator] Authentication error - check SHIPPING_API_KEY
[Shipping Calculator] Network error - carrier API may be unavailable
[Shipping Calculator] No rates returned from API, using estimates
[Shipping Calculator] PO Box detected - filtering to USPS onlyIf you always see "Falling back to estimate-based rates", check:
SHIPPING_API_KEYis set and valid- Origin address environment variables are configured
- The carrier API endpoint is reachable
Audit Log Inspection
All admin actions are logged. Query the audit log for debugging:
const logs = await prisma.auditLog.findMany({
where: {
entityType: 'Order',
action: 'create',
},
orderBy: { createdAt: 'desc' },
take: 10,
})The audit log includes: userId, action, entityType, entityId, changes (JSON), ipAddress, and userAgent.
Build Errors
If the build fails:
npm run clean # Clear caches
npm run db:generate # Regenerate Prisma client
npm run type-check # Check for type errors
npm run build # Retry buildBuild Without Database
The Prisma client is designed to initialize without a database URL during build time. It will log warnings but allow the build to succeed. Runtime access will fail if DATABASE_URL is not available.
How is this guide?
Last updated on