Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 8535232

Browse files
committed
feat: better error message on ipfs cat
`ipfs cat <hash>/wrong-dir/wrong-file` previously returned: 'No such file'. this commit changes the error message to: 'no file named "wrong-dir/wrong-file" under <hash>' License: MIT Signed-off-by: Nitin Patel <[email protected]>
1 parent 1c07779 commit 8535232

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/core/components/files-regular/cat-pull-stream.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,40 @@ module.exports = function (self) {
1414
options = options || {}
1515

1616
ipfsPath = normalizePath(ipfsPath)
17+
1718
const pathComponents = ipfsPath.split('/')
18-
const fileNameOrHash = pathComponents[pathComponents.length - 1]
19+
const cid = pathComponents[0]
1920

2021
if (options.preload !== false) {
2122
self._preload(pathComponents[0])
2223
}
2324

2425
const d = deferred.source()
2526

27+
let closestMatchedLink
28+
2629
pull(
27-
exporter(ipfsPath, self._ipld, options),
28-
pull.filter(file => file.path === fileNameOrHash),
30+
exporter(cid, self._ipld, options),
31+
pull.filter(link => {
32+
return link.path === ipfsPath.substring(0, link.path.length)
33+
}),
34+
pull.filter(link => {
35+
// save the closest matched path so we can show in error if no file was found
36+
if (!closestMatchedLink || link.depth > closestMatchedLink.depth) {
37+
closestMatchedLink = link
38+
}
39+
40+
return link.path === ipfsPath
41+
}),
2942
pull.take(1),
3043
pull.collect((err, files) => {
3144
if (err) {
3245
return d.abort(err)
3346
}
3447

3548
if (!files.length) {
36-
return d.abort(new Error('No such file'))
49+
const linkNotFound = ipfsPath.substring(closestMatchedLink.path.length + 1)
50+
return d.abort(new Error(`no file named "${linkNotFound}" under ${closestMatchedLink.path}`))
3751
}
3852

3953
const file = files[0]

test/cli/files.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ describe('files', () => runOnAndOff((thing) => {
436436
.then(() => expect.fail(0, 1, 'Should have thrown an error'))
437437
.catch((err) => {
438438
expect(err).to.exist()
439+
expect(err.stdout).to.contain('no file named "dummy" under QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
440+
})
441+
})
442+
443+
it('cat non-existent file (nested)', () => {
444+
return ipfs('cat Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z/init-docs/wrong-dir/dummy')
445+
.then(() => expect.fail(0, 1, 'Should have thrown an error'))
446+
.catch((err) => {
447+
expect(err).to.exist()
448+
expect(err.stdout).to.contain('no file named "wrong-dir/dummy" under Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z/init-docs')
439449
})
440450
})
441451

0 commit comments

Comments
 (0)