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

ipfs.dht.findProvs() can't catch error with fake hash #928

Closed
Dendiom opened this issue Jan 16, 2019 · 4 comments · Fixed by #1003
Closed

ipfs.dht.findProvs() can't catch error with fake hash #928

Dendiom opened this issue Jan 16, 2019 · 4 comments · Fixed by #1003

Comments

@Dendiom
Copy link

Dendiom commented Jan 16, 2019

We want to implement a 'exist' function to judge if provided hash is exist in the private ipfs network. We use ipfs.dht.findProvs() to achieve this logic like this:

ipfs.dht.findProvs(hash)
.then(resp => {...})
.catch(e => {...})

but we found that if the hash is valid but not exist, it will throw an error that can't catch in promise:
Uncaught TypeError: Cannot read property 'Type' of undefined
after see the source code, we found this:

const handleResult = (res, callback) => {
      // Inconsistent return values in the browser vs node
      if (Array.isArray(res)) {
        res = res[0]
      }

      // Type 4 keys
      if (res.Type !== 4) {
        const errMsg = `key was not found (type 4)`

        return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
      }
.....

It means that the res is undefined when hash is not exist?
or would you please give us a better way to achieve the 'exist' function

@alanshaw
Copy link
Contributor

@Dendiom would you be willing to submit a PR to callback with an empty array if res is null?

@KrishnaPG
Copy link
Contributor

Looks like this still does not handle the case where res is empty array. For example, in one case we are receiving empty array (for whatever reason), and it is causing error:

TypeError: Cannot read property 'Type' of undefined
    at handleResult (\node_modules\ipfs-http-client\src\dht\findprovs.js:38:15)
    at streamToValueWithTransformer (\node_modules\ipfs-http-client\src\utils\stream-to-value-with-transformer.js:14:5)
    at send (\node_modules\ipfs-http-client\src\dht\findprovs.js:68:7)
    at f (\node_modules\once\once.js:25:25)
    at concat (\node_modules\ipfs-http-client\src\utils\stream-to-value.js:12:22)
    at ConcatStream.<anonymous> (\node_modules\concat-stream\index.js:32:43)
    at ConcatStream.emit (events.js:187:15)
    at finishMaybe (\node_modules\readable-stream\lib\_stream_writable.js:620:14)
    at endWritable (\node_modules\readable-stream\lib\_stream_writable.js:629:3)
    at ConcatStream.Writable.end (\node_modules\readable-stream\lib\_stream_writable.js:567:22)

When res = [] it turns out at line 34 res = res[0] causes it become undefined leading to the above exception.

image

@alanshaw
Copy link
Contributor

@KrishnaPG are you able to submit a fix for this?

@KrishnaPG
Copy link
Contributor

Thank you - yes, array is being checked and sampled after the individual element checks line 33.

Moving the array sampling to before fixes this. PR: #1003

alanshaw pushed a commit that referenced this issue May 17, 2019
Correcting the array check to happen before the individual element checks. fixes #928
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants