Skip to content

Commit

Permalink
storage: catch errors when loading sync states
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed Aug 16, 2024
1 parent 9f10cdc commit c9e8a3b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/automerge-repo/src/storage/StorageSubsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,13 @@ export class StorageSubsystem {
storageId: StorageId
): Promise<A.SyncState | undefined> {
const key = [documentId, "sync-state", storageId]
const loaded = await this.#storageAdapter.load(key)
return loaded ? A.decodeSyncState(loaded) : undefined
try {
const loaded = await this.#storageAdapter.load(key)
return loaded ? A.decodeSyncState(loaded) : undefined
} catch (e) {
this.#log(`Error loading sync state for ${documentId} from ${storageId}`)
return undefined
}
}

async saveSyncState(
Expand Down
17 changes: 17 additions & 0 deletions packages/automerge-repo/test/StorageSubsystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ describe("StorageSubsystem", () => {
)
assert.strictEqual(loadedSyncState, undefined)
})

it("returns a undefined if loading an existing sync state fails", async () => {
const storage = new StorageSubsystem(adapter)

const { documentId } = parseAutomergeUrl(generateAutomergeUrl())
const bobStorageId = Uuid.v4() as StorageId

const syncStateKey = [documentId, "sync-state", bobStorageId]
// Save garbage data to simulate a corrupted sync state
await adapter.save(syncStateKey, Buffer.from("invalid data"))

const loadedSyncState = await storage.loadSyncState(
documentId,
bobStorageId
)
assert.strictEqual(loadedSyncState, undefined)
})
})

describe("storage id", () => {
Expand Down

0 comments on commit c9e8a3b

Please sign in to comment.