Skip to content

Commit 66118a6

Browse files
[JAVA-292] fixes WriteResult.getLastError().throwOnError()
1 parent 49072a6 commit 66118a6

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

src/main/com/mongodb/CommandResult.java

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,59 @@ public String getErrorMessage(){
6060
* @return
6161
*/
6262
public MongoException getException(){
63-
String cmdName = _cmd.keySet().iterator().next();
63+
if ( !ok() ) {
64+
String cmdName = _cmd.keySet().iterator().next();
6465

65-
StringBuilder buf = new StringBuilder( "command failed [" );
66-
buf.append( "command failed [" ).append( cmdName ).append( "] " );
67-
buf.append( toString() );
66+
StringBuilder buf = new StringBuilder( "command failed [" );
67+
buf.append( "command failed [" ).append( cmdName ).append( "] " );
68+
buf.append( toString() );
69+
70+
return new CommandFailure( this , buf.toString() );
71+
} else {
72+
// GLE check
73+
if ( hasErr() ) {
74+
Object foo = get( "err" );
75+
76+
int code = getCode();
77+
78+
String s = foo.toString();
79+
if ( code == 11000 || code == 11001 || s.startsWith( "E11000" ) || s.startsWith( "E11001" ) )
80+
return new MongoException.DuplicateKey( code , s );
81+
82+
return new MongoException( code , s );
83+
}
84+
}
6885

69-
return new CommandFailure( this , buf.toString() );
86+
//all good, should never get here.
87+
return null;
88+
}
89+
90+
/**
91+
* returns the "code" field, as an int
92+
* @return -1 if there is no code
93+
*/
94+
private int getCode(){
95+
int code = -1;
96+
if ( get( "code" ) instanceof Number )
97+
code = ((Number)get("code")).intValue();
98+
return code;
99+
}
100+
101+
/**
102+
* check the "err" field
103+
* @return if it has it, and isn't null
104+
*/
105+
boolean hasErr(){
106+
Object o = get( "err" );
107+
return (o != null && ( (String) o ).length() > 0 );
70108
}
71109

72110
/**
73-
* throws an exception containing the cmd name, in case the command failed
111+
* throws an exception containing the cmd name, in case the command failed, or the "err/code" information
74112
* @throws MongoException
75113
*/
76-
public void throwOnError()
77-
throws MongoException {
78-
if ( ! ok() ){
114+
public void throwOnError() throws MongoException {
115+
if ( !ok() || hasErr() ){
79116
throw getException();
80117
}
81118
}

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
package com.mongodb;
2020

21-
import java.io.*;
22-
import java.net.*;
21+
import java.io.IOException;
2322
import java.util.*;
24-
import java.util.logging.*;
25-
26-
import org.bson.*;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
2725

2826
public class DBTCPConnector implements DBConnector {
2927

@@ -120,20 +118,12 @@ WriteResult _checkWriteError( DB db , MyPort mp , DBPort port , WriteConcern con
120118
throws MongoException, IOException {
121119
CommandResult e = null;
122120
e = port.runCommand( db , concern.getCommand() );
123-
124-
Object foo = e.get( "err" );
125-
if ( foo == null )
121+
122+
if ( ! e.hasErr() )
126123
return new WriteResult( e , concern );
127124

128-
int code = -1;
129-
if ( e.get( "code" ) instanceof Number )
130-
code = ((Number)e.get("code")).intValue();
131-
String s = foo.toString();
132-
if ( code == 11000 || code == 11001 ||
133-
s.startsWith( "E11000" ) ||
134-
s.startsWith( "E11001" ) )
135-
throw new MongoException.DuplicateKey( code , s );
136-
throw new MongoException( code , s );
125+
e.throwOnError();
126+
return null;
137127
}
138128

139129
public WriteResult say( DB db , OutMessage m , WriteConcern concern )

src/test/com/mongodb/DBCursorTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package com.mongodb;
1818

19-
import java.io.*;
20-
import java.util.*;
19+
import java.io.IOException;
20+
import java.util.Iterator;
2121

2222
import org.testng.annotations.Test;
2323

24-
import com.mongodb.util.*;
24+
import com.mongodb.util.TestCase;
2525

2626
public class DBCursorTest extends TestCase {
2727

@@ -226,6 +226,14 @@ public void testSpecial(){
226226

227227
}
228228

229+
// @Test
230+
// public void testFullRangeCursorIdsOnClose(){
231+
// DBCollection c = _db.getCollection( "testCursorIds" );
232+
// c.insert( new BasicDBObject( "x" , 1 ) );
233+
//
234+
// DBCursor curr
235+
//
236+
// }
229237
@Test
230238
public void testLimitAndBatchSize() {
231239
DBCollection c = _db.getCollection( "LimitAndBatchSize" );

0 commit comments

Comments
 (0)