Permalink
Browse files

JAVA-757: Adding a new MongoException subclass called WriteConcernExc…

…eption, which DuplicateKey now extends. WriteConcernException takes the command result and passes toString() of it to MongoException, so the JSON representation of the entire document will appear in logs, instead of just the "err" field
  • Loading branch information...
1 parent 1c632bc commit 046d662c42d586df8e28648ee2b56044bee16b2e @jyemin jyemin committed Feb 12, 2013
@@ -25,14 +25,9 @@
public class CommandResult extends BasicDBObject {
CommandResult(ServerAddress srv) {
- this(null, srv);
- }
-
- CommandResult(DBObject cmd, ServerAddress srv) {
if (srv == null) {
throw new IllegalArgumentException("server address is null");
}
- _cmd = cmd;
_host = srv;
//so it is shown in toString/debug
put("serverUsed", srv.toString());
@@ -61,55 +56,36 @@ public boolean ok(){
* @return The error message or null
*/
public String getErrorMessage(){
- Object foo = get( "errmsg" );
- if ( foo == null )
+ Object errorMessage = get( "errmsg" );
+ if ( errorMessage == null )
return null;
- return foo.toString();
+ return errorMessage.toString();
}
/**
* utility method to create an exception with the command name
* @return The mongo exception or null
*/
- public MongoException getException(){
- if ( !ok() ) {
- StringBuilder buf = new StringBuilder();
-
- String cmdName;
- if (_cmd != null) {
- cmdName = _cmd.keySet().iterator().next();
- buf.append( "command failed [" ).append( cmdName ).append( "]: " );
- } else {
- buf.append( "operation failed: ");
+ public MongoException getException() {
+ if ( !ok() ) { // check for command failure
+ return new CommandFailureException( this );
+ } else if ( hasErr() ) { // check for errors reported by getlasterror command
+ if (getCode() == 11000 || getCode() == 11001 || getCode() == 12582) {
+ return new MongoException.DuplicateKey(this);
}
-
- buf.append( toString() );
-
- return new CommandFailure( this , buf.toString() );
- } else {
- // GLE check
- if ( hasErr() ) {
- Object foo = get( "err" );
-
- int code = getCode();
-
- String s = foo.toString();
- if ( code == 11000 || code == 11001 || s.startsWith( "E11000" ) || s.startsWith( "E11001" ) )
- return new MongoException.DuplicateKey( code , s );
-
- return new MongoException( code , s );
+ else {
+ return new MongoException.WriteConcernException(this);
}
}
- //all good, should never get here.
- return null;
+ throw new IllegalStateException("This method should not be called if there is no exception");
}
/**
* returns the "code" field, as an int
* @return -1 if there is no code
*/
- int getCode(){
+ int getCode() {
int code = -1;
if ( get( "code" ) instanceof Number )
code = ((Number)get("code")).intValue();
@@ -139,20 +115,24 @@ public ServerAddress getServerUsed() {
return _host;
}
- private final DBObject _cmd;
private final ServerAddress _host;
private static final long serialVersionUID = 1L;
- static class CommandFailure extends MongoException {
+ static class CommandFailureException extends MongoException {
private static final long serialVersionUID = 1L;
+ private final CommandResult commandResult;
/**
*
- * @param res the result
- * @param msg the message
+ * @param commandResult the result
*/
- public CommandFailure( CommandResult res , String msg ){
- super( ServerError.getCode( res ) , msg );
+ public CommandFailureException(CommandResult commandResult){
+ super(ServerError.getCode(commandResult), commandResult.toString());
+ this.commandResult = commandResult;
+ }
+
+ public CommandResult getCommandResult() {
+ return commandResult;
}
}
}
@@ -266,7 +266,7 @@ public CommandResult command( DBObject cmd , int options, ReadPreference readPre
DBObject res = i.next();
ServerAddress sa = (i instanceof Result) ? ((Result) i).getServerAddress() : null;
- CommandResult cr = new CommandResult(cmd, sa);
+ CommandResult cr = new CommandResult(sa);
cr.putAll( res );
return cr;
}
@@ -629,20 +629,20 @@ private CommandResultPair authenticateCommandHelper(String username, char[] pass
try {
authenticationTestCommandResult = doAuthenticate(credentials);
return new CommandResultPair(authenticationTestCommandResult);
- } catch (CommandResult.CommandFailure commandFailure) {
- return new CommandResultPair(commandFailure);
+ } catch (CommandResult.CommandFailureException commandFailureException) {
+ return new CommandResultPair(commandFailureException);
}
}
class CommandResultPair {
CommandResult result;
- CommandResult.CommandFailure failure;
+ CommandResult.CommandFailureException failure;
public CommandResultPair(final CommandResult result) {
this.result = result;
}
- public CommandResultPair(final CommandResult.CommandFailure failure) {
+ public CommandResultPair(final CommandResult.CommandFailureException failure) {
this.failure = failure;
}
}
@@ -109,7 +109,7 @@ public Response call( DB db , DBCollection coll , OutMessage m ,
*
* @param credentials the credentials.
* @return the result of the authentication command, if successful
- * @throws com.mongodb.CommandResult.CommandFailure if the authentication failed
+ * @throws com.mongodb.CommandResult.CommandFailureException if the authentication failed
* @since 2.11.0
*/
public CommandResult authenticate(MongoCredential credentials);
@@ -177,7 +177,7 @@ private CommandResult convertToCommandResult(DBObject cmd, Response res) {
if ( data == null )
throw new MongoInternalException( "something is wrong, no command result" );
- CommandResult cr = new CommandResult(cmd, res.serverUsed());
+ CommandResult cr = new CommandResult(res.serverUsed());
cr.putAll( data );
return cr;
}
@@ -119,19 +119,38 @@ public Network( java.io.IOException ioe ){
}
/**
- * Subclass of MongoException representing a duplicate key exception
+ * An exception representing an error reported due to a write failure.
*/
- public static class DuplicateKey extends MongoException {
+ public static class WriteConcernException extends MongoException {
- private static final long serialVersionUID = -4415279469780082174L;
+ private static final long serialVersionUID = 841056799207039974L;
+
+ private final CommandResult commandResult;
+
+ public WriteConcernException(final CommandResult commandResult) {
+ super(commandResult.getCode(), commandResult.toString());
+ this.commandResult = commandResult;
+ }
/**
- *
- * @param code the error code
- * @param msg the message
+ * Gets the getlasterror command result document.
+ *
+ * @return the command result
*/
- public DuplicateKey( int code , String msg ){
- super( code , msg );
+ public CommandResult getCommandResult() {
+ return commandResult;
+ }
+ }
+
+ /**
+ * Subclass of WriteConcernException representing a duplicate key error
+ */
+ public static class DuplicateKey extends WriteConcernException {
+
+ private static final long serialVersionUID = -4415279469780082174L;
+
+ public DuplicateKey(final CommandResult commandResult) {
+ super(commandResult);
}
}
@@ -23,12 +23,29 @@
public class CommandResultTest extends TestCase {
@Test
- public void testNullCommand() throws UnknownHostException {
- CommandResult commandResult = new CommandResult(null, new ServerAddress("localhost"));
+ public void testNullErrorCode() throws UnknownHostException {
+ CommandResult commandResult = new CommandResult(new ServerAddress("localhost"));
commandResult.put("ok", 0);
- MongoException e = commandResult.getException();
- assertNotNull(e);
- assert(e instanceof CommandResult.CommandFailure);
- assertEquals(-5, e.getCode());
+ try {
+ commandResult.throwOnError();
+ fail("Should throw");
+ } catch (CommandResult.CommandFailureException e) {
+ assertEquals(commandResult, e.getCommandResult());
+ assertEquals(-5, e.getCode());
+ }
+ }
+
+ @Test
+ public void testCommandFailure() throws UnknownHostException {
+ CommandResult commandResult = new CommandResult(new ServerAddress("localhost"));
+ final DBObject result = new BasicDBObject("ok", 0.0).append("errmsg", "no not found").append("code", 5000);
+ commandResult.putAll(result);
+ try {
+ commandResult.throwOnError();
+ fail("Should throw");
+ } catch (CommandResult.CommandFailureException e) {
+ assertEquals(commandResult, e.getCommandResult());
+ assertEquals(5000, e.getCode());
+ }
}
}
@@ -76,7 +76,8 @@ public void testDuplicateKeyException() {
Assert.fail();
}
catch (MongoException.DuplicateKey e) {
- // Proves that a DuplicateKey exception is thrown, as test will fail if any other exception is thrown
+ assertNotNull(e.getCommandResult());
+ assertEquals(11000, e.getCode());
}
}
@@ -58,7 +58,7 @@ public void testAuthentication() throws IOException {
try {
port.checkAuth(m);
fail("should throw");
- } catch (CommandResult.CommandFailure e) {
+ } catch (CommandResult.CommandFailureException e) {
// all good
}
}
@@ -144,6 +144,8 @@ public void testEnsureConnection() throws UnknownHostException {
}
}
+
+
/*public static class Person extends DBObject {
public Person(){
@@ -694,7 +694,7 @@ public void testAuthenticateCommand() throws UnknownHostException {
try {
db.authenticateCommand( "xx" , "f".toCharArray());
fail("Auth should have failed");
- } catch (CommandResult.CommandFailure e) {
+ } catch (CommandResult.CommandFailureException e) {
// all good
}
assertTrue(db.authenticateCommand("xx", "e".toCharArray()).ok());

0 comments on commit 046d662

Please sign in to comment.