DEBUG SYNC ERRORS

Ecosistema completo para organización y gestión de contenido con interfaces espaciales

🔍 Enhanced Error Logging - Debug Guide

Date: November 18, 2025
Purpose: Help identify the exact cause of sync failures


📋 What Changed

Added detailed logging to SyncEngineService.syncOperationToBackend() to capture:

  1. HTTP Status Code (httpStatus)
  2. HTTP Status Text (httpStatusText)
  3. Error Message (errorMessage)
  4. Error Details (errorDetails)
  5. Operation Type (space vs universe)
  6. Operation Action (create/update/delete)
  7. Data validation state

🔍 What to Look For in Console

Step-by-Step Logging

When a sync operation runs, you'll now see:

[SyncEngine] 🔄 Syncing operation to backend: { operation: "update", entity: "universe", key: "universe_..." }
[SyncEngine] 🌌 Processing universe operation
[SyncEngine] 🌌 Universe sync details: { operation: "update", universeId: "...", spaceId: "...", universeName: "..." }
[SyncEngine] 📤 Calling SpaceApiService.updateUniverse

If successful:

[SpaceApiService] Updating universe: { spaceId: "...", universeId: "...", name: "..." }
✅ Universe updated: universe_...
[SyncEngine] ✅ Universe updated in backend: { universeId: "...", spaceId: "...", name: "..." }

If it fails, you'll see:

[SpaceApiService] Failed to update universe: HttpErrorResponse {...}
[SyncEngine] ❌ Failed to sync operation: {
  operation: "update",
  entity: "universe",
  key: "universe_...",
  hasData: true,
  dataType: "object",
  httpStatus: 401,                    // ← KEY: The HTTP status code
  httpStatusText: "Unauthorized",     // ← KEY: The status text
  errorMessage: "...",                // ← KEY: The error message
  errorDetails: "..."                 // ← KEY: Backend error details
}

🎯 Key Information to Check

1. HTTP Status Code

Code Meaning Likely Cause
401 Unauthorized Token missing/expired/invalid
403 Forbidden User lacks permission
404 Not Found Endpoint doesn't exist or ID wrong
422 Unprocessable Entity Data validation failed
500 Internal Server Error Backend crash/bug

2. Which Operation is Failing?

Look for the last successful log before the error:

If you see:

[SyncEngine] 📤 Calling SpaceApiService.updateSpace
❌ Failed to sync operation: { entity: "space", ... }

→ Space update failing

If you see:

[SyncEngine] 📤 Calling SpaceApiService.createUniverse
❌ Failed to sync operation: { entity: "universe", operation: "create", ... }

→ Universe creation failing

If you see:

[SyncEngine] 📤 Calling SpaceApiService.updateUniverse
❌ Failed to sync operation: { entity: "universe", operation: "update", ... }

→ Universe update failing

3. Data Validation

If you see these errors BEFORE the HTTP call:

❌ Cannot sync space: missing space.id in operation.data
❌ Cannot sync universe: operation.data is undefined
❌ Cannot sync universe ...: missing spaceId in data

→ Corrupted sync queue operation (will be auto-removed)


🧪 Testing Steps

Step 1: Clear Browser Cache

  1. Open DevTools → Application → Local Storage
  2. Find syncQueue key
  3. Check if there are old operations from before the fix
  4. If yes, delete them manually OR clear all local storage

Step 2: Create a New Universe

  1. Click "New Universe"
  2. Name it: test-error-logging
  3. Create it
  4. Watch console carefully

Step 3: Analyze the Logs

Look for this sequence:

✅ GOOD: Complete success
------------------------------------------------------------
[SyncEngine] 🔄 Syncing operation to backend: { operation: "create", entity: "universe" }
[SyncEngine] 🌌 Processing universe operation
[SyncEngine] 🌌 Universe sync details: { operation: "create", universeId: "...", spaceId: "...", universeName: "test-error-logging" }
[SyncEngine] 📤 Calling SpaceApiService.createUniverse
🔐 Auth Interceptor: Added JWT token to request
[SpaceApiService] Creating universe: { spaceId: "...", universeId: "...", name: "test-error-logging" }
[SpaceApiService] ✅ Universe created: universe_...
[SyncEngine] ✅ Universe created in backend
❌ BAD: Auth failure
------------------------------------------------------------
[SyncEngine] 📤 Calling SpaceApiService.createUniverse
[SpaceApiService] Failed to create universe: HttpErrorResponse
[SyncEngine] ❌ Failed to sync operation: {
  httpStatus: 401,
  httpStatusText: "Unauthorized",
  errorMessage: "Unauthorized",
  errorDetails: { message: "Invalid token" }  ← Look here!
}
❌ BAD: Validation failure
------------------------------------------------------------
[SyncEngine] 🌌 Processing universe operation
[SyncEngine] ❌ Failed to sync operation: {
  httpStatus: "N/A",
  errorMessage: "Cannot sync universe ...: missing spaceId in data"
}

🔧 Troubleshooting Guide

If you see httpStatus: 401

Problem: Authentication token issue

Solutions:

  1. Check if logged in: localStorage.getItem('auth_token')
  2. Check token expiry: Decode JWT at jwt.io
  3. Try logging out and back in
  4. Check if AuthService is being used (not BackendAuthAdapterService)

If you see httpStatus: 404

Problem: Endpoint doesn't exist or wrong ID

Check:

  1. Is backend deployed to Railway?
  2. Is the endpoint URL correct in logs?
  3. Does the spaceId/universeId exist in MongoDB?

If you see httpStatus: 500

Problem: Backend error

Check:

  1. Railway logs for backend errors
  2. MongoDB connection status
  3. Backend console for stack traces

If you see errorMessage: "Cannot sync ..."

Problem: Corrupted sync queue operation

Solution:


📊 Success Checklist

After fixes, you should see:


📝 Report Template

When reporting errors, please provide:

**HTTP Status**: [from httpStatus field]
**Operation**: [create/update/delete]
**Entity**: [space/universe]
**Error Message**: [from errorMessage field]
**Error Details**: [from errorDetails field]
**Full Log Sequence**: [copy entire sequence from console]

Example:

**HTTP Status**: 401
**Operation**: update
**Entity**: universe
**Error Message**: Unauthorized
**Error Details**: { message: "Invalid token" }
**Full Log Sequence**:
[SyncEngine] 📤 Calling SpaceApiService.updateUniverse
[SpaceApiService] Failed to update universe: HttpErrorResponse
[SyncEngine] ❌ Failed to sync operation: { httpStatus: 401, ... }

🎯 Next Steps

  1. Clear local storage to remove old sync queue
  2. Try creating a universe and watch console
  3. Share the console logs showing:
    • httpStatus
    • errorMessage
    • errorDetails
  4. Check Network tab in DevTools for request/response details

With this enhanced logging, we can now pinpoint the EXACT cause of any sync failure! 🎯