fix: handle Document unavailable error with try-catch in repo.find()

When repo.find() is called for a document that exists on the server but not
locally, it throws 'Document unavailable' error. This fix:

- Wraps repo.find() in try-catch block
- Falls back to creating new handle if document not found
- Allows sync adapter to merge with server state via network

This handles the case where clients join existing rooms and need to sync
documents from the network.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2025-11-16 20:33:31 -07:00
parent 2cc7c52755
commit 02808d170e
1 changed files with 12 additions and 4 deletions

View File

@ -146,10 +146,18 @@ export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus
let handle: DocHandle<TLStoreSnapshot> let handle: DocHandle<TLStoreSnapshot>
if (documentId) { if (documentId) {
// Find the existing document (will sync from network if not available locally) // Try to find the existing document
console.log(`🔍 Finding document ${documentId} (will sync from network if needed)`) console.log(`🔍 Attempting to find document ${documentId}`)
handle = await repo.find<TLStoreSnapshot>(documentId as any) try {
console.log(`✅ Got handle for document: ${documentId}`) handle = await repo.find<TLStoreSnapshot>(documentId as any)
console.log(`✅ Found document handle: ${documentId}`)
} catch (error) {
// Document not available yet - this can happen when it exists on server but not locally
console.log(`📝 Document ${documentId} not immediately available, creating new handle`)
// Create a new handle - the sync will handle merging with server state
handle = repo.create<TLStoreSnapshot>()
console.log(`📝 Created new handle ${handle.documentId}, will sync with server`)
}
} else { } else {
// Create a new document and register its ID with the server // Create a new document and register its ID with the server
handle = repo.create<TLStoreSnapshot>() handle = repo.create<TLStoreSnapshot>()