Skip to content

Commit 0fcde32

Browse files
committed
Docs: Added error handling notes to README, see #696
1 parent d89c45f commit 0fcde32

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ protobuf.load("awesome.proto", function(err, root) {
111111
// Create a new message
112112
var message = AwesomeMessage.create({ awesomeField: "AwesomeString" });
113113

114+
// Verify the message if necessary (i.e. when possibly incomplete or invalid)
115+
var err = AwesomeMessage.verify(message);
116+
if (err)
117+
throw Error(err);
118+
114119
// Encode a message to an Uint8Array (browser) or Buffer (node)
115120
var buffer = AwesomeMessage.encode(message).finish();
116121
// ... do something with buffer
@@ -127,7 +132,9 @@ protobuf.load("awesome.proto", function(err, root) {
127132
});
128133
```
129134

130-
You can also use promises by omitting the callback:
135+
**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.
136+
137+
Additionally, promise syntax can be used by omitting the callback, if preferred:
131138

132139
```js
133140
protobuf.load("awesome.proto")

0 commit comments

Comments
 (0)