Browse files

Docs: Added error handling notes to README, see #696

  • Loading branch information...
dcodeIO committed Mar 5, 2017
1 parent d89c45f commit 0fcde32306da77f02cb1ea81ed18a32cee01f17b
Showing with 8 additions and 1 deletion.
  1. +8 −1
@@ -111,6 +111,11 @@ protobuf.load("awesome.proto", function(err, root) {
// Create a new message
var message = AwesomeMessage.create({ awesomeField: "AwesomeString" });
// Verify the message if necessary (i.e. when possibly incomplete or invalid)
var err = AwesomeMessage.verify(message);
if (err)
throw Error(err);
// Encode a message to an Uint8Array (browser) or Buffer (node)
var buffer = AwesomeMessage.encode(message).finish();
// ... do something with buffer
@@ -127,7 +132,9 @@ protobuf.load("awesome.proto", function(err, root) {
You can also use promises by omitting the callback:
**Note** that `Message.encode` does not verify a message but tries to encode whatever is specified, which might result in a runtime error being thrown somewhere down the road. Instead, there is `Message.verify` to explicitly perform verification priorly (only) where necessary to avoid redundant assertions where messages are already known to be valid. `Message.decode` throws if a buffer is invalid.
Additionally, promise syntax can be used by omitting the callback, if preferred:

0 comments on commit 0fcde32

Please sign in to comment.