From bfef65763c511dbfd43799a73c73933888c50a0f Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Sat, 23 Jan 2016 10:24:02 -0600 Subject: [PATCH] Layout: refine exceptions Change a few TypeError to RangeError, and finesse the fact that some TypeErrors might be unacceptable values. Closes #12. --- lib/Layout.js | 16 ++++++++-------- test/LayoutTest.js | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/Layout.js b/lib/Layout.js index c615155..819b9c3 100644 --- a/lib/Layout.js +++ b/lib/Layout.js @@ -382,7 +382,7 @@ function GreedyCount(elementSpan, property) { elementSpan = 1; } if ((!Number.isInteger(elementSpan)) || (0 >= elementSpan)) { - throw new TypeError('elementSpan must be a positive integer'); + throw new TypeError('elementSpan must be a (positive) integer'); } ExternalLayout.call(this, -1, property); @@ -500,7 +500,7 @@ OffsetLayout.prototype.encode = function(src, b, offset) { function UInt(span, property) { Layout.call(this, span, property); if (6 < this.span) { - throw new TypeError('span must not exceed 6 bytes'); + throw new RangeError('span must not exceed 6 bytes'); } Object.freeze(this); } @@ -540,7 +540,7 @@ UInt.prototype.encode = function(src, b, offset) { function UIntBE(span, property) { Layout.call(this, span, property); if (6 < this.span) { - throw new TypeError('span must not exceed 6 bytes'); + throw new RangeError('span must not exceed 6 bytes'); } Object.freeze(this); } @@ -580,7 +580,7 @@ UIntBE.prototype.encode = function(src, b, offset) { function Int(span, property) { Layout.call(this, span, property); if (6 < this.span) { - throw new TypeError('span must not exceed 6 bytes'); + throw new RangeError('span must not exceed 6 bytes'); } Object.freeze(this); } @@ -620,7 +620,7 @@ Int.prototype.encode = function(src, b, offset) { function IntBE(span, property) { Layout.call(this, span, property); if (6 < this.span) { - throw new TypeError('span must not exceed 6 bytes'); + throw new RangeError('span must not exceed 6 bytes'); } Object.freeze(this); } @@ -1617,7 +1617,7 @@ function VariantLayout(union, throw new TypeError('union must be a Union'); } if ((!Number.isInteger(variant)) || (0 > variant)) { - throw new TypeError('variant must be a non-negative integer'); + throw new TypeError('variant must be a (non-negative) integer'); } if (!(layout instanceof Layout)) { throw new TypeError('layout must be a Layout'); @@ -1765,7 +1765,7 @@ function BitStructure(word, msb, property) { throw new TypeError('word must be a UInt or UIntBE layout'); } if (4 < word.span) { - throw new Error('word cannot exceed 32 bits'); + throw new RangeError('word cannot exceed 32 bits'); } Layout.call(this, word.span, property); @@ -2066,7 +2066,7 @@ Blob.prototype.encode = function(src, b, offset) { } if (!((src instanceof Buffer) && (span === src.length))) { - throw new Error('Blob.encode requires length ' + span + ' Buffer as src'); + throw new Error('Blob.encode requires (length ' + span + ') Buffer as src'); } if ((offset + span) > b.length) { throw new RangeError('encoding overruns Buffer'); diff --git a/test/LayoutTest.js b/test/LayoutTest.js index 3fab90c..18a2ea2 100644 --- a/test/LayoutTest.js +++ b/test/LayoutTest.js @@ -113,7 +113,7 @@ suite('Layout', function() { assert.equal(0xA534, d.decode(b, 2)); }); test('invalid ctor', function() { - assert.throws(function() { new lo.UInt(8); }, TypeError); + assert.throws(function() { new lo.UInt(8); }, RangeError); }); }); suite('UIntBE', function() { @@ -176,7 +176,7 @@ suite('Layout', function() { assert.equal(0x34A5, d.decode(b, 2)); }); test('invalid ctor', function() { - assert.throws(function() { new lo.UIntBE(8); }, TypeError); + assert.throws(function() { new lo.UIntBE(8); }, RangeError); }); }); suite('Int', function() { @@ -255,7 +255,7 @@ suite('Layout', function() { assert.equal(lo.u48be().decode(b), 0x8720f279b78f); }); test('invalid ctor', function() { - assert.throws(function() { new lo.Int(8); }, TypeError); + assert.throws(function() { new lo.Int(8); }, RangeError); }); }); suite('IntBE', function() { @@ -328,7 +328,7 @@ suite('Layout', function() { assert.equal(lo.u48().decode(b), 0x8720f279b78f); }); test('invalid ctor', function() { - assert.throws(function() { new lo.IntBE(8, 'u64'); }, TypeError); + assert.throws(function() { new lo.IntBE(8, 'u64'); }, RangeError); }); }); test('RoundedUInt64', function() {