Skip to content

Commit

Permalink
fix: handle ipfs.files.stat RPC error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Winter-Soren committed Feb 9, 2025
1 parent 7fbb51b commit 7602904
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/bundles/files/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
}
}

Expand All @@ -123,9 +130,9 @@ const stat = async (ipfs, cidOrPath) => {
* @param {IPFSService} ipfs
* @returns {AsyncIterable<Pin>}
*/
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' })
}

/**
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/bundles/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7602904

Please sign in to comment.