diff --git a/lib/index.js b/lib/index.js index f50521f..036be1b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -32,6 +32,18 @@ function initGC(peer, config) { let stopMonitoringLogSize = /** @type {CallableFunction | null} */ (null) let hasCleanupScheduled = false + /** + * @param {Error} err + */ + function flattenCauseChain(err) { + let str = '' + while (err?.message ?? err) { + str += (err.message ?? err) + ': ' + err = /**@type {Error}*/ (err.cause) + } + return str + } + /** * Deletes messages that don't correspond to any goal. * @private @@ -40,12 +52,15 @@ function initGC(peer, config) { function cleanup(cb) { debug('Cleanup started') const startTime = Date.now() - const done = multicb({ pluck: 1 }) - function makeRecCB(/**@type {string}*/ errorMessage) { + + /** + * @param {string} explanation + */ + function makeRecCB(explanation) { const cb = done() return (/**@type {Error=}*/ err) => { - if (err) debug('%s: %s', errorMessage, err.message ?? err) + if (err) debug('%s: %s', explanation, flattenCauseChain(err)) cb() } } @@ -58,12 +73,14 @@ function initGC(peer, config) { if (purpose === 'goal') continue // don't cleanup if (purpose === 'none') { const recCB = makeRecCB('Failed to delete msg when cleaning up') + debug('Deleting msg %s with purpose=none', msgID) peer.db.del(msgID, recCB) waiting = true } else if (purpose === 'ghost') { const { tangleID, span } = details const recCB = makeRecCB('Failed to delete ghost msg when cleaning up') // TODO: Could one msg be a ghostable in MANY tangles? Or just one? + debug('Deleting and ghosting msg %s with purpose=ghost', msgID) peer.db.ghosts.add({ tangleID, msgID, span }, (err) => { if (err) return recCB(err) peer.db.del(msgID, recCB) @@ -71,6 +88,7 @@ function initGC(peer, config) { waiting = true } else if (purpose === 'trail') { const recCB = makeRecCB('Failed to erase trail msg when cleaning up') + debug('Erasing msg %s with purpose=trail', msgID) peer.db.erase(msgID, recCB) waiting = true } else {