Skip to content

Commit e068b5c

Browse files
hhromicmbroadst
authored andcommitted
refactor(errors): useTypeError for function parameters errors
[this PR is a rebase (and update) of PR #157] For example, when constructing a new `ObjectId` object, the passed id value is validated for conversion to an hex string. This is just fine, however a generic Error object is thrown when this validation fails. Tracking the error then becomes unconvenient. From official [Joyent's design patterns for errors](https://www.joyent.com/developers/node/design/errors), it is suggested that: > 3. Use the Error's name property to distinguish errors programmatically. > When you need to figure out what kind of error this is, use the name property. > Built-in JavaScript names you may want to reuse include "RangeError" > (an argument is outside of its valid range) and "TypeError" (an argument has the wrong type). > For HTTP errors, it's common to use the RFC-given status text to name the error, like > "BadRequestError" or "ServiceUnavailableError". This PR uses the [`TypeError` class](https://nodejs.org/api/errors.html#errors_class_typeerror) instead of the generic `Error` object. Because `TypeError` is a subclass of `Error`, all existing code should continue to work flawlessly and new code can more easily track an invalid argument error. Signed-off-by: Hugo Hromic <[email protected]>
1 parent 9c9e027 commit e068b5c

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

alternate_parsers/bson.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(
526526
*/
527527
var serializeObject = function(object, checkKeys, buffer, index, serializeFunctions) {
528528
if (object.toBSON) {
529-
if (typeof object.toBSON !== 'function') throw new Error('toBSON is not a function');
529+
if (typeof object.toBSON !== 'function') throw new TypeError('toBSON is not a function');
530530
object = object.toBSON();
531531
if (object != null && typeof object !== 'object')
532532
throw new Error('toBSON function did not return an object');
@@ -1289,7 +1289,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
12891289
BSON.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
12901290
// Throw error if we are trying serialize an illegal type
12911291
if (object == null || typeof object !== 'object' || Array.isArray(object))
1292-
throw new Error('Only javascript objects supported');
1292+
throw new TypeError('Only javascript objects supported');
12931293

12941294
// Emoty target buffer
12951295
var buffer = null;

lib/bson/binary.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function Binary(buffer, subType) {
5252
) {
5353
this.buffer = writeStringToArray(buffer);
5454
} else {
55-
throw new Error('only String, Buffer, Uint8Array or Array accepted');
55+
throw new TypeError('only String, Buffer, Uint8Array or Array accepted');
5656
}
5757
} else {
5858
this.buffer = buffer;
@@ -80,9 +80,9 @@ function Binary(buffer, subType) {
8080
Binary.prototype.put = function put(byte_value) {
8181
// If it's a string and a has more than one character throw an error
8282
if (byte_value['length'] != null && typeof byte_value !== 'number' && byte_value.length !== 1)
83-
throw new Error('only accepts single character String, Uint8Array or Array');
83+
throw new TypeError('only accepts single character String, Uint8Array or Array');
8484
if ((typeof byte_value !== 'number' && byte_value < 0) || byte_value > 255)
85-
throw new Error('only accepts number in a valid unsigned byte range 0-255');
85+
throw new TypeError('only accepts number in a valid unsigned byte range 0-255');
8686

8787
// Decode the byte value once
8888
var decoded_byte = null;

lib/bson/decimal128.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ var lessThan = function(left, right) {
142142
};
143143

144144
var invalidErr = function(string, message) {
145-
throw new Error('"${string}" not a valid Decimal128 string - ' + message);
145+
throw new TypeError('"${string}" not a valid Decimal128 string - ' + message);
146146
};
147147

148148
/**
@@ -210,7 +210,7 @@ Decimal128.fromString = function(string) {
210210
// TODO: implementing a custom parsing for this, or refactoring the regex would yield
211211
// further gains.
212212
if (string.length >= 7000) {
213-
throw new Error('' + string + ' not a valid Decimal128 string');
213+
throw new TypeError('' + string + ' not a valid Decimal128 string');
214214
}
215215

216216
// Results
@@ -220,7 +220,7 @@ Decimal128.fromString = function(string) {
220220

221221
// Validate the string
222222
if ((!stringMatch && !infMatch && !nanMatch) || string.length === 0) {
223-
throw new Error('' + string + ' not a valid Decimal128 string');
223+
throw new TypeError('' + string + ' not a valid Decimal128 string');
224224
}
225225

226226
if (stringMatch) {
@@ -291,7 +291,7 @@ Decimal128.fromString = function(string) {
291291
index = index + 1;
292292
}
293293

294-
if (sawRadix && !nDigitsRead) throw new Error('' + string + ' not a valid Decimal128 string');
294+
if (sawRadix && !nDigitsRead) throw new TypeError('' + string + ' not a valid Decimal128 string');
295295

296296
// Read exponent if exists
297297
if (string[index] === 'e' || string[index] === 'E') {

lib/bson/objectid.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ObjectID(id) {
5050

5151
// Throw an error if it's not a valid setup
5252
if (!valid && id != null) {
53-
throw new Error(
53+
throw new TypeError(
5454
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
5555
);
5656
} else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
@@ -64,7 +64,7 @@ function ObjectID(id) {
6464
// Duck-typing to support ObjectId from different npm packages
6565
return id;
6666
} else {
67-
throw new Error(
67+
throw new TypeError(
6868
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
6969
);
7070
}
@@ -92,7 +92,7 @@ ObjectID.prototype.toHexString = function() {
9292

9393
var hexString = '';
9494
if (!this.id || !this.id.length) {
95-
throw new Error(
95+
throw new TypeError(
9696
'invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [' +
9797
JSON.stringify(this.id) +
9898
']'
@@ -302,7 +302,7 @@ var convertToHex = function(bytes) {
302302
ObjectID.createFromHexString = function createFromHexString(string) {
303303
// Throw an error if it's not a valid setup
304304
if (typeof string === 'undefined' || (string != null && string.length !== 24)) {
305-
throw new Error(
305+
throw new TypeError(
306306
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
307307
);
308308
}

lib/bson/parser/serializer.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ var serializeObjectId = function(buffer, key, value, index, isArray) {
286286
} else if (value.id && value.id.copy) {
287287
value.id.copy(buffer, index, 0, 12);
288288
} else {
289-
throw new Error('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
289+
throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
290290
}
291291

292292
// Ajust index
@@ -684,7 +684,7 @@ var serializeInto = function serializeInto(
684684

685685
// Is there an override value
686686
if (value && value.toBSON) {
687-
if (typeof value.toBSON !== 'function') throw new Error('toBSON is not a function');
687+
if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
688688
value = value.toBSON();
689689
}
690690

@@ -863,18 +863,18 @@ var serializeInto = function serializeInto(
863863
} else {
864864
// Did we provide a custom serialization method
865865
if (object.toBSON) {
866-
if (typeof object.toBSON !== 'function') throw new Error('toBSON is not a function');
866+
if (typeof object.toBSON !== 'function') throw new TypeError('toBSON is not a function');
867867
object = object.toBSON();
868868
if (object != null && typeof object !== 'object')
869-
throw new Error('toBSON function did not return an object');
869+
throw new TypeError('toBSON function did not return an object');
870870
}
871871

872872
// Iterate over all the keys
873873
for (key in object) {
874874
value = object[key];
875875
// Is there an override value
876876
if (value && value.toBSON) {
877-
if (typeof value.toBSON !== 'function') throw new Error('toBSON is not a function');
877+
if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
878878
value = value.toBSON();
879879
}
880880

0 commit comments

Comments
 (0)