Skip to content

Commit

Permalink
Layout: refine exceptions
Browse files Browse the repository at this point in the history
Change a few TypeError to RangeError, and finesse the fact that some
TypeErrors might be unacceptable values.

Closes #12.
  • Loading branch information
pabigot committed Jan 23, 2016
1 parent 208ca5c commit bfef657
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions test/LayoutTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit bfef657

Please sign in to comment.