Skip to content

Commit bf39492

Browse files
author
Ruben Bridgewater
committed
Use built-in error classes to make errors more specific
1 parent 5368e74 commit bf39492

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

lib/createClient.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function createClient (port_arg, host_arg, options) {
1212
host = host_arg;
1313
} else {
1414
if (options && host_arg) {
15-
throw new Error('Unknown type of connection in createClient()');
15+
throw new TypeError('Unknown type of connection in createClient()');
1616
}
1717
options = options || host_arg;
1818
}
@@ -50,14 +50,14 @@ module.exports = function createClient (port_arg, host_arg, options) {
5050
if (options[elem] === parsed.query[elem]) {
5151
console.warn('node_redis: WARNING: You passed the ' + elem + ' option twice!');
5252
} else {
53-
throw new Error('The ' + elem + ' option is added twice and does not match');
53+
throw new RangeError('The ' + elem + ' option is added twice and does not match');
5454
}
5555
}
5656
options[elem] = parsed.query[elem];
5757
}
5858
}
5959
} else if (parsed.hostname) {
60-
throw new Error('The redis url must begin with slashes "//" or contain slashes after the redis protocol');
60+
throw new RangeError('The redis url must begin with slashes "//" or contain slashes after the redis protocol');
6161
} else {
6262
options.path = port_arg;
6363
}
@@ -67,12 +67,12 @@ module.exports = function createClient (port_arg, host_arg, options) {
6767
options.host = options.host || host_arg;
6868

6969
if (port_arg && arguments.length !== 1) {
70-
throw new Error('To many arguments passed to createClient. Please only pass the options object');
70+
throw new TypeError('To many arguments passed to createClient. Please only pass the options object');
7171
}
7272
}
7373

7474
if (!options) {
75-
throw new Error('Unknown type of connection in createClient()');
75+
throw new TypeError('Unknown type of connection in createClient()');
7676
}
7777

7878
return options;

lib/extendedApi.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ All documented and exposed API belongs in here
1313
RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = function (command, args, callback) {
1414
// Throw to fail early instead of relying in order in this case
1515
if (typeof command !== 'string') {
16-
throw new Error('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name');
16+
throw new TypeError('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name');
1717
}
1818
if (!Array.isArray(args)) {
1919
if (args === undefined || args === null) {
@@ -22,11 +22,11 @@ RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = functio
2222
callback = args;
2323
args = [];
2424
} else {
25-
throw new Error('Wrong input type "' + args.constructor.name + '" for args');
25+
throw new TypeError('Wrong input type "' + args.constructor.name + '" for args');
2626
}
2727
}
2828
if (typeof callback !== 'function' && callback !== undefined) {
29-
throw new Error('Wrong input type "' + (callback !== null ? callback.constructor.name : 'null') + '" for callback function');
29+
throw new TypeError('Wrong input type "' + (callback !== null ? callback.constructor.name : 'null') + '" for callback function');
3030
}
3131

3232
// Using the raw multi command is only possible with this function

lib/multi.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function multi_callback (self, err, replies) {
4646

4747
if (err) {
4848
err.errors = self.errors;
49-
err.command = 'EXEC';
5049
if (self.callback) {
5150
self.callback(err);
5251
// Exclude connection errors so that those errors won't be emitted twice
@@ -86,7 +85,7 @@ function multi_callback (self, err, replies) {
8685

8786
Multi.prototype.exec_transaction = function exec_transaction (callback) {
8887
if (this.monitoring || this._client.monitoring) {
89-
var err = new Error(
88+
var err = new RangeError(
9089
'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.'
9190
);
9291
err.command = 'EXEC';

0 commit comments

Comments
 (0)