From 0fcde32306da77f02cb1ea81ed18a32cee01f17b Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sun, 5 Mar 2017 21:41:17 +0100 Subject: [PATCH] Docs: Added error handling notes to README, see #696 --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aa02ac71e..8b09251db 100644 --- a/README.md +++ b/README.md @@ -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: ```js protobuf.load("awesome.proto")