diff options
-rw-r--r-- | jstests/sharding/replset_config/bad_config_load.js | 10 | ||||
-rw-r--r-- | src/mongo/shell/collection.js | 16 | ||||
-rw-r--r-- | src/mongo/shell/db.js | 60 | ||||
-rw-r--r-- | src/mongo/shell/explainable.js | 2 | ||||
-rw-r--r-- | src/mongo/shell/mongo.js | 6 | ||||
-rw-r--r-- | src/mongo/shell/query.js | 11 | ||||
-rw-r--r-- | src/mongo/shell/shardingtest.js | 2 | ||||
-rw-r--r-- | src/mongo/shell/utils.js | 20 | ||||
-rw-r--r-- | src/mongo/shell/utils_sh.js | 6 |
9 files changed, 74 insertions, 59 deletions
diff --git a/jstests/sharding/replset_config/bad_config_load.js b/jstests/sharding/replset_config/bad_config_load.js index 63028c55565..8b9573c4451 100644 --- a/jstests/sharding/replset_config/bad_config_load.js +++ b/jstests/sharding/replset_config/bad_config_load.js @@ -40,12 +40,10 @@ for( var i = 0; i < 2; i++ ){ printjson( e ) // Make sure we get a transport error, and not a no-primary error - // Unfortunately e gets stringified so we have to test this way - assert(e.message.indexOf("10276") >= 0 || // Transport error - e.message.indexOf("13328") >= 0 || // Connect error - e.message.indexOf("13639") >= 0 || // Connect error to replSet primary - e.message.indexOf("network error") >= 0 || - e.message.indexOf("socket") >= 0 ) + assert(e.code == 10276 || // Transport error + e.code == 13328 || // Connect error + e.code == 13639 || // Connect error to replSet primary + e.code == 6); // Host unreachable } } diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js index 5ecfd01cc31..596c03db920 100644 --- a/src/mongo/shell/collection.js +++ b/src/mongo/shell/collection.js @@ -204,7 +204,7 @@ DBCollection.prototype.findOne = function( query , fields, options ){ var ret = cursor.next(); if ( cursor.hasNext() ) throw Error( "findOne has more than 1 result!" ); if ( ret.$err ) - throw Error( "error " + tojson( ret ) ); + throw _getErrorWithCode(ret, "error " + tojson(ret)); return ret; } @@ -621,7 +621,7 @@ DBCollection.prototype.dropIndexes = function(){ if ( res.errmsg.match( /not found/ ) ) return res; - throw Error( "error dropping indexes : " + tojson( res ) ); + throw _getErrorWithCode(res, "error dropping indexes : " + tojson(res)); } @@ -632,7 +632,7 @@ DBCollection.prototype.drop = function(){ if ( ! ret.ok ){ if ( ret.errmsg == "ns not found" ) return false; - throw Error( "drop failed: " + tojson( ret ) ); + throw _getErrorWithCode(ret, "drop failed: " + tojson(ret)); } return true; } @@ -648,7 +648,7 @@ DBCollection.prototype.findAndModify = function(args){ if (ret.errmsg == "No matching object found"){ return null; } - throw Error( "findAndModifyFailed failed: " + tojson( ret ) ); + throw _getErrorWithCode(ret, "findAndModifyFailed failed: " + tojson(ret)); } return ret.value; } @@ -1031,7 +1031,7 @@ DBCollection.prototype._getIndexesCommand = function(filter){ return null; } - throw Error( "listIndexes failed: " + tojson( res ) ); + throw _getErrorWithCode(ret, "listIndexes failed: " + tojson(res)); } return new DBCommandCursor(this._mongo, res).toArray(); @@ -1228,7 +1228,7 @@ DBCollection.prototype.exists = function(){ return this._db.system.namespaces.findOne( { name : this._fullName } ); } - throw Error( "listCollections failed: " + tojson( res ) ); + throw _getErrorWithCode(res, "listCollections failed: " + tojson(res)); } DBCollection.prototype.isCapped = function(){ @@ -1249,7 +1249,7 @@ DBCollection.prototype.distinct = function( keyString , query ){ throw Error("The query argument to the distinct command must be a document but was a " + queryType); var res = this._distinct( keyString , query ); if ( ! res.ok ) - throw Error("distinct failed: " + tojson( res )); + throw _getErrorWithCode(res, "distinct failed: " + tojson(res)); return res.values; } @@ -1386,7 +1386,7 @@ DBCollection.prototype.mapReduce = function( map , reduce , optionsOrOutString ) if ( ! raw.ok ){ __mrerror__ = raw; - throw Error( "map reduce failed:" + tojson(raw) ); + throw _getErrorWithCode(raw, "map reduce failed:" + tojson(raw)); } return new MapReduceResult( this._db , raw ); diff --git a/src/mongo/shell/db.js b/src/mongo/shell/db.js index 2e0755d703f..2cecf28e579 100644 --- a/src/mongo/shell/db.js +++ b/src/mongo/shell/db.js @@ -40,7 +40,7 @@ DB.prototype.commandHelp = function( name ){ c.help = true; var res = this.runCommand( c ); if ( ! res.ok ) - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); return res.help; } @@ -238,7 +238,7 @@ DB.prototype.getProfilingLevel = function() { DB.prototype.getProfilingStatus = function() { var res = this._dbCommand( { profile: -1 } ); if ( ! res.ok ) - throw Error( "profile command failed: " + tojson( res ) ); + throw _getErrorWithCode(res, "profile command failed: " + tojson(res)); delete res.ok return res; } @@ -277,7 +277,7 @@ DB.prototype.shutdownServer = function(opts) { try { var res = this.runCommand(cmd); if (!res.ok) { - throw Error('shutdownServer failed: ' + tojson(res)); + throw _getErrorWithCode(res, 'shutdownServer failed: ' + tojson(res)); } throw Error('shutdownServer failed: server is still up.'); } @@ -539,7 +539,7 @@ DB.prototype.eval = function(jsfunction) { var res = this._dbCommand( cmd ); if (!res.ok) - throw Error( tojson( res ) ); + throw _getErrorWithCode(res, tojson(res)); return res.retval; } @@ -623,7 +623,7 @@ DB.prototype.groupeval = function(parmsObj) { DB.prototype.groupcmd = function( parmsObj ){ var ret = this.runCommand( { "group" : this._groupFixParms( parmsObj ) } ); if ( ! ret.ok ){ - throw Error( "group command failed: " + tojson( ret ) ); + throw _getErrorWithCode(ret, "group command failed: " + tojson(ret)); } return ret.retval; } @@ -657,7 +657,7 @@ DB.prototype.forceError = function(){ DB.prototype.getLastError = function( w , wtimeout ){ var res = this.getLastErrorObj( w , wtimeout ); if ( ! res.ok ) - throw Error( "getlasterror failed: " + tojson( res ) ); + throw _getErrorWithCode(ret, "getlasterror failed: " + tojson(res)); return res.err; } DB.prototype.getLastErrorObj = function( w , wtimeout ){ @@ -670,7 +670,7 @@ DB.prototype.getLastErrorObj = function( w , wtimeout ){ var res = this.runCommand( cmd ); if ( ! res.ok ) - throw Error( "getlasterror failed: " + tojson( res ) ); + throw _getErrorWithCode(res, "getlasterror failed: " + tojson(res)); return res; } DB.prototype.getLastErrorCmd = DB.prototype.getLastErrorObj; @@ -732,7 +732,7 @@ DB.prototype._getCollectionInfosCommand = function(filter) { return null; } - throw Error( "listCollections failed: " + tojson( res ) ); + throw _getErrorWithCode(res, "listCollections failed: " + tojson(res)); } // The listCollections command returns its results sorted by collection name. There's no need @@ -1210,7 +1210,7 @@ DB.prototype.createUser = function(userObj, writeConcern) { "database will not be fully secured until replication finishes"); } - throw Error("couldn't add user: " + res.errmsg); + throw _getErrorWithCode(res, "couldn't add user: " + res.errmsg); } function _hashPassword(username, password) { @@ -1239,9 +1239,9 @@ DB.prototype._updateUserV1 = function(name, updateObject, writeConcern) { this.system.users.update({user : name, userSource : null}, {$set : setObj}); - var err = this.getLastError(writeConcern['w'], writeConcern['wtimeout']); - if (err) { - throw Error("Updating user failed: " + err); + var errObj = this.getLastErrorObj(writeConcern['w'], writeConcern['wtimeout']); + if (errObj.err) { + throw _getErrorWithCode(errObj, "Updating user failed: " + errObj.err); } }; @@ -1261,7 +1261,7 @@ DB.prototype.updateUser = function(name, updateObject, writeConcern) { return; } - throw Error("Updating user failed: " + res.errmsg); + throw _getErrorWithCode(res, "Updating user failed: " + res.errmsg); }; DB.prototype.changeUserPassword = function(username, password, writeConcern) { @@ -1295,7 +1295,7 @@ DB.prototype.dropUser = function( username, writeConcern ){ return this._removeUserV1(username, cmdObj['writeConcern']); } - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } /** @@ -1308,7 +1308,7 @@ DB.prototype._removeUserV1 = function(username, writeConcern) { var le = this.getLastErrorObj(writeConcern['w'], writeConcern['wtimeout']); if (le.err) { - throw Error( "Couldn't remove user: " + le.err ); + throw _getErrorWithCode(le, "Couldn't remove user: " + le.err); } if (le.n == 1) { @@ -1323,7 +1323,7 @@ DB.prototype.dropAllUsers = function(writeConcern) { writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } return res.n; @@ -1407,7 +1407,7 @@ DB.prototype.grantRolesToUser = function(username, roles, writeConcern) { writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } } @@ -1417,7 +1417,7 @@ DB.prototype.revokeRolesFromUser = function(username, roles, writeConcern) { writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } } @@ -1430,7 +1430,7 @@ DB.prototype.getUser = function(username, args) { var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } if (res.users.length == 0) { @@ -1451,7 +1451,7 @@ DB.prototype.getUsers = function(args) { return this.system.users.find({}).toArray(); } - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } return res.users; @@ -1467,7 +1467,7 @@ DB.prototype.createRole = function(roleObj, writeConcern) { var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } printjson(roleObj); } @@ -1478,7 +1478,7 @@ DB.prototype.updateRole = function(name, updateObject, writeConcern) { cmdObj['writeConcern'] = writeConcern ? writeConcern : _defaultWriteConcern; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } }; @@ -1495,7 +1495,7 @@ DB.prototype.dropRole = function(name, writeConcern) { return false; } - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); }; DB.prototype.dropAllRoles = function(writeConcern) { @@ -1503,7 +1503,7 @@ DB.prototype.dropAllRoles = function(writeConcern) { writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } return res.n; @@ -1515,7 +1515,7 @@ DB.prototype.grantRolesToRole = function(rolename, roles, writeConcern) { writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } } @@ -1525,7 +1525,7 @@ DB.prototype.revokeRolesFromRole = function(rolename, roles, writeConcern) { writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } } @@ -1535,7 +1535,7 @@ DB.prototype.grantPrivilegesToRole = function(rolename, privileges, writeConcern writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } } @@ -1545,7 +1545,7 @@ DB.prototype.revokePrivilegesFromRole = function(rolename, privileges, writeConc writeConcern: writeConcern ? writeConcern : _defaultWriteConcern}; var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } } @@ -1557,7 +1557,7 @@ DB.prototype.getRole = function(rolename, args) { Object.extend(cmdObj, args); var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } if (res.roles.length == 0) { @@ -1571,7 +1571,7 @@ DB.prototype.getRoles = function(args) { Object.extend(cmdObj, args); var res = this.runCommand(cmdObj); if (!res.ok) { - throw Error(res.errmsg); + throw _getErrorWithCode(res, res.errmsg); } return res.roles; diff --git a/src/mongo/shell/explainable.js b/src/mongo/shell/explainable.js index 7d6438fc7b3..68d83643ed8 100644 --- a/src/mongo/shell/explainable.js +++ b/src/mongo/shell/explainable.js @@ -31,7 +31,7 @@ var Explainable = (function() { var throwOrReturn = function(explainResult) { if (("ok" in explainResult && !explainResult.ok) || explainResult.$err) { - throw Error("explain failed: " + tojson(explainResult)); + throw _getErrorWithCode(explainResult, "explain failed: " + tojson(explainResult)); } return explainResult; diff --git a/src/mongo/shell/mongo.js b/src/mongo/shell/mongo.js index 29100a14db3..fb6e63583a3 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -50,7 +50,7 @@ Mongo.prototype.getDB = function( name ){ Mongo.prototype.getDBs = function(){ var res = this.getDB( "admin" ).runCommand( { "listDatabases" : 1 } ); if ( ! res.ok ) - throw Error( "listDatabases failed:" + tojson( res ) ); + throw _getErrorWithCode(res, "listDatabases failed:" + tojson(res)); return res; } @@ -64,7 +64,7 @@ Mongo.prototype.adminCommand = function( cmd ){ Mongo.prototype.getLogComponents = function() { var res = this.adminCommand({ getParameter:1, logComponentVerbosity:1 }); if (!res.ok) - throw Error( "getLogComponents failed:" + tojson(res)); + throw _getErrorWithCode(res, "getLogComponents failed:" + tojson(res)); return res.logComponentVerbosity; } @@ -91,7 +91,7 @@ Mongo.prototype.setLogLevel = function(logLevel, component) { } var res = this.adminCommand({ setParameter : 1, logComponentVerbosity : vDoc }); if (!res.ok) - throw Error( "setLogLevel failed:" + tojson(res)); + throw _getErrorWithCode(res, "setLogLevel failed:" + tojson(res)); return res; } diff --git a/src/mongo/shell/query.js b/src/mongo/shell/query.js index 866a0d8e590..616c2319a81 100644 --- a/src/mongo/shell/query.js +++ b/src/mongo/shell/query.js @@ -265,8 +265,9 @@ DBQuery.prototype.next = function(){ throw Error( "error hasNext: " + o ); var ret = this._cursor.next(); - if ( ret.$err ) - throw Error( "error: " + tojson( ret ) ); + if ( ret.$err ) { + throw _getErrorWithCode(ret, "error: " + tojson( ret )); + } this._numReturned++; return ret; @@ -277,7 +278,7 @@ DBQuery.prototype.objsLeftInBatch = function(){ var ret = this._cursor.objsLeftInBatch(); if ( ret.$err ) - throw Error( "error: " + tojson( ret ) ); + throw _getErrorWithCode(ret, "error: " + tojson( ret )); return ret; } @@ -333,7 +334,7 @@ DBQuery.prototype.count = function( applySkipLimit ) { var res = this._db.runCommand( cmd ); if( res && res.n != null ) return res.n; - throw Error( "count failed: " + tojson( res ) ); + throw _getErrorWithCode(res, "count failed: " + tojson( res )); } DBQuery.prototype.size = function(){ @@ -618,7 +619,7 @@ DBCommandCursor.prototype.next = function() { var ret = this._cursor.next(); if ( ret.$err ) - throw Error( "error: " + tojson( ret ) ); + throw _getErrorWithCode(ret, "error: " + tojson(ret)); return ret; } } diff --git a/src/mongo/shell/shardingtest.js b/src/mongo/shell/shardingtest.js index 79b7049fc9a..4238f8c2a83 100644 --- a/src/mongo/shell/shardingtest.js +++ b/src/mongo/shell/shardingtest.js @@ -616,7 +616,7 @@ ShardingTest.prototype.adminCommand = function(cmd){ if ( res && res.ok == 1 ) return true; - throw Error( "command " + tojson( cmd ) + " failed: " + tojson( res ) ); + throw _getErrorWithCode(res, "command " + tojson(cmd) + " failed: " + tojson(res)); } ShardingTest.prototype._rangeToString = function(r){ diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js index 19fce1a8446..5600a0cab11 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -19,6 +19,22 @@ function reconnect(db) { }); }; +function _getErrorWithCode(codeOrObj, message) { + var e = new Error(message); + if (codeOrObj != undefined) { + if (codeOrObj.writeError) { + e.code = codeOrObj.writeError.code; + } else if (codeOrObj.code) { + e.code = codeOrObj.code; + } else { + // At this point assume codeOrObj is a number type + e.code = codeOrObj; + } + } + + return e; +} + // Please consider using bsonWoCompare instead of this as much as possible. friendlyEqual = function( a , b ){ if ( a == b ) @@ -316,7 +332,7 @@ replSetMemberStatePrompt = function() { if ( info && info.length < 20 ) { state = info; // "mongos", "configsvr" } else { - throw Error("Failed:" + info); + throw _getErrorWithCode(stateInfo, "Failed:" + info); } } return state + '> '; @@ -346,7 +362,7 @@ isMasterStatePrompt = function() { } state = state + role; } else { - throw Error("Failed: " + tojson(isMaster)); + throw _getErrorWithCode(isMaster, "Failed: " + tojson(isMaster)); } return state + '> '; } diff --git a/src/mongo/shell/utils_sh.js b/src/mongo/shell/utils_sh.js index ae4fa06d7c6..64b539f5816 100644 --- a/src/mongo/shell/utils_sh.js +++ b/src/mongo/shell/utils_sh.js @@ -333,9 +333,9 @@ sh._lastMigration = function( ns ){ } sh._checkLastError = function( mydb ) { - var err = mydb.getLastError(); - if ( err ) - throw Error( "error: " + err ); + var errObj = mydb.getLastErrorObj(); + if (errObj.err) + throw _getErrorWithCode(errObj, "error: " + errObj.err); } sh.addShardTag = function( shard, tag ) { |