From bf394923fd548419928ee9108aa87bc16a6e8d33 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 25 Apr 2016 01:35:56 +0200 Subject: [PATCH] Use built-in error classes to make errors more specific --- lib/createClient.js | 10 +++++----- lib/extendedApi.js | 6 +++--- lib/multi.js | 3 +-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/createClient.js b/lib/createClient.js index 72d5e2745c..a019fc7a38 100644 --- a/lib/createClient.js +++ b/lib/createClient.js @@ -12,7 +12,7 @@ module.exports = function createClient (port_arg, host_arg, options) { host = host_arg; } else { if (options && host_arg) { - throw new Error('Unknown type of connection in createClient()'); + throw new TypeError('Unknown type of connection in createClient()'); } options = options || host_arg; } @@ -50,14 +50,14 @@ module.exports = function createClient (port_arg, host_arg, options) { if (options[elem] === parsed.query[elem]) { console.warn('node_redis: WARNING: You passed the ' + elem + ' option twice!'); } else { - throw new Error('The ' + elem + ' option is added twice and does not match'); + throw new RangeError('The ' + elem + ' option is added twice and does not match'); } } options[elem] = parsed.query[elem]; } } } else if (parsed.hostname) { - throw new Error('The redis url must begin with slashes "//" or contain slashes after the redis protocol'); + throw new RangeError('The redis url must begin with slashes "//" or contain slashes after the redis protocol'); } else { options.path = port_arg; } @@ -67,12 +67,12 @@ module.exports = function createClient (port_arg, host_arg, options) { options.host = options.host || host_arg; if (port_arg && arguments.length !== 1) { - throw new Error('To many arguments passed to createClient. Please only pass the options object'); + throw new TypeError('To many arguments passed to createClient. Please only pass the options object'); } } if (!options) { - throw new Error('Unknown type of connection in createClient()'); + throw new TypeError('Unknown type of connection in createClient()'); } return options; diff --git a/lib/extendedApi.js b/lib/extendedApi.js index 44dc9e2823..ef6a2faa6e 100644 --- a/lib/extendedApi.js +++ b/lib/extendedApi.js @@ -13,7 +13,7 @@ All documented and exposed API belongs in here RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = function (command, args, callback) { // Throw to fail early instead of relying in order in this case if (typeof command !== 'string') { - throw new Error('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name'); + throw new TypeError('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name'); } if (!Array.isArray(args)) { if (args === undefined || args === null) { @@ -22,11 +22,11 @@ RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = functio callback = args; args = []; } else { - throw new Error('Wrong input type "' + args.constructor.name + '" for args'); + throw new TypeError('Wrong input type "' + args.constructor.name + '" for args'); } } if (typeof callback !== 'function' && callback !== undefined) { - throw new Error('Wrong input type "' + (callback !== null ? callback.constructor.name : 'null') + '" for callback function'); + throw new TypeError('Wrong input type "' + (callback !== null ? callback.constructor.name : 'null') + '" for callback function'); } // Using the raw multi command is only possible with this function diff --git a/lib/multi.js b/lib/multi.js index 2a7431e9b6..433c45bd50 100644 --- a/lib/multi.js +++ b/lib/multi.js @@ -46,7 +46,6 @@ function multi_callback (self, err, replies) { if (err) { err.errors = self.errors; - err.command = 'EXEC'; if (self.callback) { self.callback(err); // Exclude connection errors so that those errors won't be emitted twice @@ -86,7 +85,7 @@ function multi_callback (self, err, replies) { Multi.prototype.exec_transaction = function exec_transaction (callback) { if (this.monitoring || this._client.monitoring) { - var err = new Error( + var err = new RangeError( 'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.' ); err.command = 'EXEC';