Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Fix for dag.get failing silently on missing multicodec #831

Merged
merged 1 commit into from
Aug 13, 2018

Conversation

lidel
Copy link
Contributor

@lidel lidel commented Aug 12, 2018

Right now ipfs.dag.get fails silently for CIDs with codecs different than dag-cbor or dag-pb.
To reproduce, try dag.get for raw one at bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy.

This PR fixes the bug by returning error when codec resolver is missing.

Notes:

  • Problem occurs only in js-ipfs-api, js-ipfs already delegates resolvers to ipld which has proper error handling here.
  • Added regression tests for dag.put+get, not sure if it is the right place tho. Any feedback would be appreciated.
  • Not sure what is the long term plan for resolvers, I assume endgame will be [WIP] feat(dag): add IPLD to dag.get #755 ?

@lidel lidel added the bug label Aug 12, 2018
@ghost ghost assigned lidel Aug 12, 2018
@ghost ghost added the in progress label Aug 12, 2018
@lidel lidel force-pushed the fix/dag-get-codec-error-handling branch from 68ee648 to 0ba96bf Compare August 12, 2018 20:16
src/dag/get.js Outdated
}
const error = new Error('ipfs-api is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec')
error.missingMulticodec = ipfsBlock.cid.codec
cb(error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, only comment is a stylistic one - usually we return early on error condition, but we're doing the opposite here. Personally I'd if (!dagResolver) return cb(new Error(/*...*/)).

test/dag.spec.js Outdated
ipfsd.stop(done)
})

it('.dag.put+get dag-pb', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love to see something a little more descriptive here...".dag.put+get dag-pb" is a bit cryptic.

e.g. "should be able to put and get a DAG node with format dag-pb"

test/dag.spec.js Outdated
})
})

it('.dag.get missing raw multicodec returns error', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a better description, could use a tiny bit more improvement:

e.g. "should callback with error when missing DAG resolver for raw multicodec"

😝

test/dag.spec.js Outdated
// CIDv1 with multicodec = raw
const cid = 'bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy'
ipfs.dag.get(cid, (err, result) => {
expect(err.message).to.equal('ipfs-api is missing DAG resolver for "raw" multicodec')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(err).to.exist() before this please :) That way if the error is undefined/null we get an error message saying that the error was expected to exist rather than "TypeError: Cannot read property 'message' of null" which is a little harder to debug.

test/dag.spec.js Outdated
const cbor = {foo: 'dag-cbor-bar'}
ipfs.dag.put(cbor, {format: 'dag-cbor', hashAlg: 'sha2-256'}, (err, cid) => {
expect(err).to.not.exist()
cid = new CID(cid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should already be a CID instance.

test/dag.spec.js Outdated
expect(err).to.not.exist()
ipfs.dag.put(node, {format: 'dag-pb', hashAlg: 'sha2-256'}, (err, cid) => {
expect(err).to.not.exist()
cid = new CID(cid).toV0()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should already be a CID instance

test/dag.spec.js Outdated
})
})

it('.dag.put+get dag-cbor', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can we have a more readable description here as well?

Before this change it just failed silently for 'raw' DAGs
such as `bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy`

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
@lidel lidel force-pushed the fix/dag-get-codec-error-handling branch from 0ba96bf to 107699d Compare August 13, 2018 12:06
@lidel
Copy link
Contributor Author

lidel commented Aug 13, 2018

@alanshaw applied suggested changes and rebased, ok to merge?

@alanshaw alanshaw merged commit ff7c7e5 into master Aug 13, 2018
@alanshaw alanshaw deleted the fix/dag-get-codec-error-handling branch August 13, 2018 14:29
@ghost ghost removed the in progress label Aug 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants