Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch error when unmarshaling instead of crashing #72

Merged
merged 8 commits into from Dec 1, 2017
Merged

Conversation

mkg20001
Copy link
Member

Releated #71

Copy link
Member

@daviddias daviddias left a comment

Choose a reason for hiding this comment

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

Thank you for adding a failing test. Are you working towards the fix as well?

@mkg20001
Copy link
Member Author

Currently more focused on libp2p-websocket-star. Will add the fix when I've released the new version (0.6.0)

@mkg20001
Copy link
Member Author

Fixed it!

src/index.js Outdated
if (err) {
return callback(err)
}
const pubKey = crypto.keys.unmarshalPublicKey(buf)
Copy link
Member

Choose a reason for hiding this comment

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

what about wrapping this line in a try/catch block?

Copy link
Member Author

@mkg20001 mkg20001 Nov 27, 2017

Choose a reason for hiding this comment

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

The whole thing is in try catch. Also included the Buffer.from because it throws an error when a non-base64 string is passed to it.

Copy link
Member

@daviddias daviddias left a comment

Choose a reason for hiding this comment

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

Needs some adjustments

src/index.js Outdated
return callback(err)
}

callback(null, new PeerId(digest, null, pubKey))
Copy link
Member

Choose a reason for hiding this comment

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

This is an anti-pattern, you should not call a callback from within a try/catch block because any future error that is thrown will circle back to this try catch.

Copy link
Member

Choose a reason for hiding this comment

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

You need to move the callback to outside of the try/catch block

src/index.js Outdated
})
} catch (e) {
callback(e)
}
Copy link
Member

Choose a reason for hiding this comment

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

Please always call errors, err so that the StandardJS linter catches them.

src/index.js Outdated
return callback(err)
}

callback(null, new PeerId(digest, privKey, privKey.public))
Copy link
Member

Choose a reason for hiding this comment

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

Same issue as above

src/index.js Outdated
})
} catch (e) {
callback(e)
}
Copy link
Member

Choose a reason for hiding this comment

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

Also same issue as above

src/index.js Outdated
})
} else {
callback(null, new PeerId(id, null, pub))
}
Copy link
Member

Choose a reason for hiding this comment

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

One more. Golden rule, make try/catch blocks as small as they can be, only wrap the call that might throw and nothing else.

src/index.js Outdated
callback(null, new PeerId(id, null, pub))
}
} catch (e) {
callback(e)
Copy link
Member

Choose a reason for hiding this comment

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

always name errors err

@mkg20001
Copy link
Member Author

Applied the requested changes but now it seems like libp2p-crypto also has the exact same issues with crypto.keys.unmarshalPrivateKey which has a cb

src/index.js Outdated
rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
pub = rawPubKey && crypto.keys.unmarshalPublicKey(rawPubKey)
} catch (err) {
callback(err)
Copy link
Member

Choose a reason for hiding this comment

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

This callback needs a return

@daviddias
Copy link
Member

@mkg20001 CI is failing, see https://circleci.com/gh/libp2p/js-peer-id/226

@mkg20001
Copy link
Member Author

Yes I see. This is because, as I previously said, the libp2p-crypto module has the exact same error. Which is something that should be handled there. libp2p/js-libp2p-crypto#112

@pgte
Copy link
Contributor

pgte commented Nov 30, 2017

All tests seam to pass when using libp2p/js-libp2p-crypto#113

@ghost ghost assigned daviddias Dec 1, 2017
@daviddias daviddias merged commit 156911e into master Dec 1, 2017
@ghost ghost removed the status/in-progress In progress label Dec 1, 2017
@daviddias daviddias deleted the fix/crash branch December 1, 2017 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants