From 7602904426babcbbff1bff364e8e5c08142cf58a Mon Sep 17 00:00:00 2001 From: Winter-Soren Date: Sun, 9 Feb 2025 18:04:40 +0530 Subject: [PATCH] fix: handle ipfs.files.stat RPC error responses --- src/bundles/files/actions.js | 43 +++++++++++++++++++++--------------- src/bundles/notify.js | 11 +++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/bundles/files/actions.js b/src/bundles/files/actions.js index d3f349a33..ff3f0ad78 100644 --- a/src/bundles/files/actions.js +++ b/src/bundles/files/actions.js @@ -101,20 +101,27 @@ const stat = async (ipfs, cidOrPath) => { } else { stats = await ipfs.files.stat(path) } - return { path, ...stats } - } catch (e) { - // Discard error and mark DAG as 'unknown' to unblock listing other pins. - // Clicking on 'unknown' entry will open it in Inspector. - // No information is lost: if there is an error related - // to specified hashOrPath user will read it in Inspector. - const [, , cid] = path.split('/') - return { - path: hashOrPath, - cid: CID.asCID(cid) ?? CID.parse(cid), - type: 'unknown', - cumulativeSize: 0, - size: 0 + + if (stats.type === 'error') { + throw Object.assign(new Error(stats.message || 'Failed to get file stats'), { + code: 'ERR_FILES_STAT_FAILED' + }) } + + return { path, ...stats } + } catch (error) { + + const e = error instanceof Error ? error : new Error('Unknown error') + throw Object.assign(e, { + code: 'ERR_FILES_STAT_FAILED', + fallback: { + path: hashOrPath, + cid: CID.asCID(path.split('/')[2]) ?? CID.parse(path.split('/')[2]), + type: 'unknown', + cumulativeSize: 0, + size: 0 + } + }) } } @@ -123,9 +130,9 @@ const stat = async (ipfs, cidOrPath) => { * @param {IPFSService} ipfs * @returns {AsyncIterable} */ -const getRawPins = async function * (ipfs) { - yield * ipfs.pin.ls({ type: 'recursive' }) - yield * ipfs.pin.ls({ type: 'direct' }) +const getRawPins = async function* (ipfs) { + yield* ipfs.pin.ls({ type: 'recursive' }) + yield* ipfs.pin.ls({ type: 'direct' }) } /** @@ -246,7 +253,7 @@ const actions = () => ({ * @param {FileStream[]} source * @param {string} root */ - doFilesWrite: (source, root) => spawn(ACTIONS.WRITE, async function * (ipfs, { store }) { + doFilesWrite: (source, root) => spawn(ACTIONS.WRITE, async function* (ipfs, { store }) { const files = source // Skip ignored files .filter($ => !IGNORED_FILES.includes(basename($.path))) @@ -335,7 +342,7 @@ const actions = () => ({ * same file) to crash webui, nor want to bother user with false-negatives * @param {Function} fn */ - const tryAsync = async fn => { try { await fn() } catch (_) {} } + const tryAsync = async fn => { try { await fn() } catch (_) { } } try { // try removing from MFS first diff --git a/src/bundles/notify.js b/src/bundles/notify.js index 7bd7ca256..f0ba78807 100644 --- a/src/bundles/notify.js +++ b/src/bundles/notify.js @@ -24,6 +24,17 @@ const notify = { return { ...state, show: false } } + if (action.type === 'FILES_STAT_FAILED') { + return { + ...state, + show: true, + error: true, + eventId: 'FILES_EVENT_FAILED', + code: action.payload.error.code, + msgArgs: { message: action.payload.error.message } + } + } + if (action.type === 'STATS_FETCH_FAILED') { return { ...state,