Protobuf.js: Error Handling

Created on 5 Mar 2017  路  4Comments  路  Source: protobufjs/protobuf.js

As far as I can tell there are no docs on error handling for this package. Are errors thrown when using decode/encode? Is there an emitter I can bind to? Maybe I just missed them while looking around?

I would be happy to write up something for the readme, or wherever you prefer. But I think as a noob with this package I would like guidance from the maintainers before I do :)

question

All 4 comments

decode throws if the buffer cannot be decoded, i.e. is invalid / not matching the schema. Starting with 6.7.0, it will also throw if the argument is not a valid buffer (see).

encode does not verify messages on its own. It just tries to encode whatever is specified, which might, for entirely bogus data, result in a runtime error being thrown somewhere down the road.

For explicit verification of messages, there is verify instead, which can be used where necessary. Having separate methods for encoding and verification avoids performing possibly redundant assertions when encoding. For example, sometimes a developer already knows that a payload is valid, thus it would be unnecessary to verify it (again) implicitly. verify does not throw but returns an error string if there are issues, or null if there are none.

Hope this helps!

That does help, thanks! Would you be opposed to me taking this explanation and adding it to the readme? Or is there a better place to document this so others don't have to dig?

I've now added respective documentation to the initial README example. Decoders now also throw if required fields are missing so that verify does not have to be called after decode, which it isn't meant for (would introduce new redundant assertions otherwise).

Related: https://github.com/dcodeIO/protobuf.js/issues/695

Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VitalyName picture VitalyName  路  3Comments

mj-mehdizadeh picture mj-mehdizadeh  路  5Comments

zD98 picture zD98  路  3Comments

chloe2018s picture chloe2018s  路  3Comments

spiderkeys picture spiderkeys  路  5Comments