Skip to content

Commit e9c3858

Browse files
committed
[JAVA-313]: several commands do not call res.throwOnError(), instead make up their own exception
1 parent 815ca33 commit e9c3858

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

src/main/com/mongodb/DB.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,8 @@ public Object eval( String code , Object ... args )
215215
throws MongoException {
216216

217217
CommandResult res = doEval( code , args );
218-
219-
if ( res.ok() ){
220-
return res.get( "retval" );
221-
}
222-
223-
throw new MongoException( "eval failed: " + res );
218+
res.throwOnError();
219+
return res.get( "retval" );
224220
}
225221

226222
/**
@@ -462,8 +458,7 @@ public CommandResult authenticateCommand(String username, char[] passwd )
462458

463459
String hash = _hash( username , passwd );
464460
CommandResult res = _doauth( username , hash.getBytes() );
465-
if ( !res.ok())
466-
throw new MongoException(res);
461+
res.throwOnError();
467462
_username = username;
468463
_authhash = hash.getBytes();
469464
return res;
@@ -499,9 +494,7 @@ static DBObject _authCommand( String nonce , String username , byte[] hash ){
499494

500495
private CommandResult _doauth( String username , byte[] hash ){
501496
CommandResult res = command(new BasicDBObject("getnonce", 1), getOptions());
502-
if ( ! res.ok() ){
503-
throw new MongoException(res);
504-
}
497+
res.throwOnError();
505498

506499
DBObject cmd = _authCommand( res.getString( "nonce" ) , username , hash );
507500
return command(cmd, getOptions());

src/main/com/mongodb/DBCollection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,8 @@ public long getCount(DBObject query, DBObject fields, long limit, long skip )
767767
// for now, return 0 - lets pretend it does exist
768768
return 0;
769769
}
770-
771-
throw new MongoException( "error counting : " + res );
770+
771+
res.throwOnError();
772772
}
773773

774774
return res.getLong("n");

src/main/com/mongodb/DBPort.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ void checkAuth( DB db ) throws IOException {
286286

287287
res = runCommand( db , temp );
288288

289-
if ( ! res.ok() )
290-
throw new MongoException( "couldn't re-auth, username/password change?" );
289+
res.throwOnError();
291290
_authed.put( db , true );
292291
}
293292

src/main/com/mongodb/Mongo.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,8 @@ public List<String> getDatabaseNames()
344344
cmd.put("listDatabases", 1);
345345

346346

347-
BasicDBObject res = (BasicDBObject)getDB( "admin" ).command(cmd, getOptions());
348-
349-
if (res.getInt("ok" , 0 ) != 1 )
350-
throw new MongoException( "error listing databases : " + res );
347+
CommandResult res = getDB( "admin" ).command(cmd, getOptions());
348+
res.throwOnError();
351349

352350
List l = (List)res.get("databases");
353351

@@ -620,4 +618,19 @@ public void run() {
620618
}
621619
}
622620
}
621+
622+
@Override
623+
public String toString() {
624+
String str = "Mongo: ";
625+
List<ServerAddress> list = getServerAddressList();
626+
if (list == null || list.isEmpty())
627+
str += "null";
628+
else {
629+
for (ServerAddress addr : list) {
630+
str += addr.toString() + ",";
631+
}
632+
str = str.substring(0, str.length() - 1);
633+
}
634+
return str;
635+
}
623636
}

src/main/com/mongodb/ServerError.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static int getCode( BSONObject o ){
3131
Object c = o.get( "code" );
3232
if ( c == null )
3333
c = o.get( "$code" );
34+
if ( c == null )
35+
c = o.get( "assertionCode" );
3436

3537
if ( c == null )
3638
return -5;

0 commit comments

Comments
 (0)