Skip to content

Commit

Permalink
buffer: include encoding value in exception when invalid
Browse files Browse the repository at this point in the history
Encoding failures can be somewhat confusing, especially when they are due to
control flow frameworks auto-filling parameters from the previous step output
values to functions (such as toString and write) that developers don't expect
to take an encoding parameter. By outputting the value as part of the message,
should make it easier to track down these sort of bugs.
  • Loading branch information
rngadam authored and bnoordhuis committed Oct 9, 2012
1 parent 5288ed7 commit 8bd4590
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SlowBuffer.prototype.toString = function(encoding, start, end) {
return this.ucs2Slice(start, end);

default:
throw new Error('Unknown encoding');
throw new Error('Unknown encoding: ' + encoding);
}
};

Expand Down Expand Up @@ -170,7 +170,7 @@ SlowBuffer.prototype.write = function(string, offset, length, encoding) {
return this.ucs2Write(string, offset, length);

default:
throw new Error('Unknown encoding');
throw new Error('Unknown encoding: ' + encoding);
}
};

Expand Down Expand Up @@ -402,7 +402,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
break;

default:
throw new Error('Unknown encoding');
throw new Error('Unknown encoding: ' + encoding);
}

Buffer._charsWritten = SlowBuffer._charsWritten;
Expand Down Expand Up @@ -459,7 +459,7 @@ Buffer.prototype.toString = function(encoding, start, end) {
return this.parent.ucs2Slice(start, end);

default:
throw new Error('Unknown encoding');
throw new Error('Unknown encoding: ' + encoding);
}
};

Expand Down
36 changes: 36 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ try {
}
assert.strictEqual('sourceStart out of bounds', caught_error.message);

// invalid encoding for Buffer.toString
caught_error = null;
try {
var copied = b.toString('invalid');
} catch (err) {
caught_error = err;
}
assert.strictEqual('Unknown encoding: invalid', caught_error.message);

// invalid encoding for Buffer.write
caught_error = null;
try {
var copied = b.write('test string', 0, 5, 'invalid');
} catch (err) {
caught_error = err;
}
assert.strictEqual('Unknown encoding: invalid', caught_error.message);

// a too-low sourceEnd will get caught by earlier checks

// try to copy ending after the end of b
Expand Down Expand Up @@ -600,6 +618,24 @@ assert.equal(0xad, b[1]);
assert.equal(0xbe, b[2]);
assert.equal(0xef, b[3]);

// testing invalid encoding on SlowBuffer.toString
caught_error = null;
try {
var copied = b.toString('invalid');
} catch (err) {
caught_error = err;
}
assert.strictEqual('Unknown encoding: invalid', caught_error.message);

// testing invalid encoding on SlowBuffer.write
caught_error = null;
try {
var copied = b.write('some string', 0, 5, 'invalid');
} catch (err) {
caught_error = err;
}
assert.strictEqual('Unknown encoding: invalid', caught_error.message);


// This should not segfault the program.
assert.throws(function() {
Expand Down

0 comments on commit 8bd4590

Please sign in to comment.