Skip to content

Commit

Permalink
Docs: Added error handling notes to README, see #696
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 5, 2017
1 parent d89c45f commit 0fcde32
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:

```js
protobuf.load("awesome.proto")
Expand Down

0 comments on commit 0fcde32

Please sign in to comment.