diff --git a/README.md b/README.md index 136a026..fd1293c 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ modified. decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer. +Throws a `RangeError` when `data` does not represent a valid encoding. + ### varint.decode.bytes if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode. @@ -41,7 +43,7 @@ returns the number of bytes this number will be encoded as, up to a maximum of 8 ## usage notes If varint is passed a buffer that does not contain a valid end -byte, then `decode` will return undefined, and `decode.bytes` +byte, then `decode` will throw `RangeError`, and `decode.bytes` will be set to 0. If you are reading from a streaming source, it's okay to pass an incomplete buffer into `decode`, detect this case, and then concatenate the next buffer. diff --git a/decode.js b/decode.js index 67768be..4d274c2 100644 --- a/decode.js +++ b/decode.js @@ -12,10 +12,9 @@ function read(buf, offset) { , l = buf.length do { - if(counter >= l) { + if (counter >= l) { read.bytes = 0 - read.bytesRead = 0 // DEPRECATED - throw new Error('Could not decode varint') + throw new RangeError('Could not decode varint') } b = buf[counter++] res += shift < 28 diff --git a/test.js b/test.js index e746562..0138ff4 100644 --- a/test.js +++ b/test.js @@ -124,10 +124,12 @@ test('buffer too short', function (assert) { var l = buffer.length while(l--) { - var val = decode(buffer.slice(0, l)) - assert.equal(val, undefined) - assert.equal(decode.bytes, 0) - assert.equal(decode.bytesRead, 0) + try { + var val = decode(buffer.slice(0, l)) + } catch (err) { + assert.equal(err.constructor, RangeError) + assert.equal(decode.bytes, 0) + } } assert.end() })