Skip to content

use more specific TypeError objects for function parameters errors #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/bson/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Binary(buffer, subType) {
} else if(typeof Uint8Array != 'undefined' || (Object.prototype.toString.call(buffer) == '[object Array]')) {
this.buffer = writeStringToArray(buffer);
} else {
throw new Error("only String, Buffer, Uint8Array or Array accepted");
throw new TypeError("only String, Buffer, Uint8Array or Array accepted");
}
} else {
this.buffer = buffer;
Expand All @@ -71,8 +71,8 @@ function Binary(buffer, subType) {
*/
Binary.prototype.put = function put(byte_value) {
// If it's a string and a has more than one character throw an error
if(byte_value['length'] != null && typeof byte_value != 'number' && byte_value.length != 1) throw new Error("only accepts single character String, Uint8Array or Array");
if(typeof byte_value != 'number' && byte_value < 0 || byte_value > 255) throw new Error("only accepts number in a valid unsigned byte range 0-255");
if(byte_value['length'] != null && typeof byte_value != 'number' && byte_value.length != 1) throw new TypeError("only accepts single character String, Uint8Array or Array");
if(typeof byte_value != 'number' && byte_value < 0 || byte_value > 255) throw new TypeError("only accepts number in a valid unsigned byte range 0-255");

// Decode the byte value once
var decoded_byte = null;
Expand Down
6 changes: 3 additions & 3 deletions lib/bson/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var ObjectID = function ObjectID(id) {

// Throw an error if it's not a valid setup
if(!valid && id != null){
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
} else if(valid && typeof id == 'string' && id.length == 24) {
return ObjectID.createFromHexString(id);
} else if(id == null || typeof id == 'number') {
Expand Down Expand Up @@ -216,12 +216,12 @@ ObjectID.createFromTime = function createFromTime (time) {
ObjectID.createFromHexString = function createFromHexString (hexString) {
// Throw an error if it's not a valid setup
if(typeof hexString === 'undefined' || hexString != null && hexString.length != 24)
throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");

var len = hexString.length;

if(len > 12*2) {
throw new Error('Id cannot be longer than 12 bytes');
throw new TypeError('Id cannot be longer than 12 bytes');
}

var result = ''
Expand Down
4 changes: 2 additions & 2 deletions lib/bson/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function BSONRegExp(pattern, options) {
|| this.options[i] == 's'
|| this.options[i] == 'u'
)) {
throw new Error('the regular expression options [' + this.options[i] + "] is not supported");
throw new TypeError('the regular expression options [' + this.options[i] + "] is not supported");
}
}
}

module.exports = BSONRegExp;
module.exports.BSONRegExp = BSONRegExp;
module.exports.BSONRegExp = BSONRegExp;