Skip to content

Commit 66b8927

Browse files
committed
fix: Resume sync with reset cache after MappingFailure
see #2096 Signed-off-by: Marcel Klehr <[email protected]>
1 parent 16afcdb commit 66b8927

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/errors/Error.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,15 @@ export class XbelParseError extends FloccusError {
411411
this.code = 47
412412
Object.setPrototypeOf(this, XbelParseError.prototype)
413413
}
414+
}
415+
416+
export class MappingFailureError extends FloccusError {
417+
public id: string
418+
419+
constructor(id: string) {
420+
super(`E048: Failed to map ID: ${id}`)
421+
this.id = id
422+
this.code = 48
423+
Object.setPrototypeOf(this, MappingFailureError.prototype)
424+
}
414425
}

src/lib/Account.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,19 @@ export default class Account {
337337
} catch (e) {
338338
console.log(e)
339339
const message = await Account.stringifyError(e)
340+
341+
// Catch MappingFailureError and gracefully resume with reset cache
342+
if (matchAllErrors(e, e => e.code === 48)) {
343+
Logger.log('Caught MappingFailureError: Gracefully resuming with reset cache')
344+
await this.init()
345+
await this.storage.setCurrentContinuation(null)
346+
this.syncProcess = null
347+
this.localCachingResource = null
348+
await this.setData({ syncing: false })
349+
this.syncing = false
350+
return this.sync(strategy, forceSync)
351+
}
352+
340353
console.error('Syncing failed with', message)
341354
Logger.log('Syncing failed with', message)
342355
setContext('accountData', {
@@ -432,5 +445,5 @@ export default class Account {
432445
}
433446

434447
function matchAllErrors(e, fn:(e)=>boolean) {
435-
return fn(e) && e.list && e.list.every(e => matchAllErrors(e, fn))
448+
return fn(e) && (e.list ? e.list.every(e => matchAllErrors(e, fn)) : true)
436449
}

src/lib/Diff.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Mappings, { MappingSnapshot } from './Mappings'
33
import Ordering from './interfaces/Ordering'
44
import batchingToposort from 'batching-toposort'
55
import Logger from './Logger'
6+
import { MappingFailureError } from '../errors/Error'
67

78
export const ActionType = {
89
CREATE: 'CREATE',
@@ -255,7 +256,7 @@ export default class Diff<L1 extends TItemLocation, L2 extends TItemLocation, A
255256
diff.commit(action)
256257
Logger.log('Failed to map parentId of action ' + diff.inspect())
257258
Logger.log(JSON.stringify(mappingsSnapshot, null,'\t'))
258-
throw new Error('Failed to map parentId to ' + targetLocation + ': ' + action.payload.parentId)
259+
throw new MappingFailureError(action.payload.parentId.toString())
259260
}
260261
}
261262
}

0 commit comments

Comments
 (0)