Skip to content

Commit 22e4616

Browse files
committed
Added MongoCommandException
CommandFailureException extends MongoCommandException and is deprecated UnacknowledgedWriteException extends UnsupportedOperationException instead of MongoClientException and is deprecated JAVA-1570
1 parent 122481c commit 22e4616

File tree

8 files changed

+59
-34
lines changed

8 files changed

+59
-34
lines changed

src/main/com/mongodb/CommandFailureException.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
/**
2020
* An exception indicating that a command sent to a MongoDB server returned a failure.
21+
* @deprecated Use {@link com.mongodb.MongoCommandException instead}.
2122
*/
22-
public class CommandFailureException extends MongoException {
23+
@Deprecated
24+
public class CommandFailureException extends MongoCommandException {
2325
private static final long serialVersionUID = -1180715413196161037L;
24-
private final CommandResult commandResult;
2526

2627
/**
2728
* Construct a new instance with the CommandResult from a failed command
@@ -31,28 +32,7 @@ public class CommandFailureException extends MongoException {
3132
*/
3233
@Deprecated
3334
public CommandFailureException(CommandResult commandResult){
34-
super(ServerError.getCode(commandResult), commandResult.toString());
35-
this.commandResult = commandResult;
36-
}
37-
38-
/**
39-
* Gets the address of the server that the command executed on.
40-
*
41-
* @return the address of the server that the command executed on
42-
* @since 2.13
43-
*/
44-
public ServerAddress getServerAddress() {
45-
return commandResult.getServerUsed();
46-
}
47-
48-
/**
49-
* Gets the error message from the command failure, typically from the "errmsg" property of the document returned from the failed
50-
* command.
51-
*
52-
* @return the error message
53-
*/
54-
public String getErrorMessage() {
55-
return commandResult.getErrorMessage();
35+
super(commandResult);
5636
}
5737

5838
/**
@@ -63,6 +43,6 @@ public String getErrorMessage() {
6343
*/
6444
@Deprecated
6545
public CommandResult getCommandResult() {
66-
return commandResult;
46+
return super.getCommandResult();
6747
}
6848
}

src/main/com/mongodb/DuplicateKeyException.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,4 @@ public class DuplicateKeyException extends WriteConcernException {
3333
DuplicateKeyException(final CommandResult commandResult) {
3434
super(commandResult);
3535
}
36-
37-
DuplicateKeyException(final int code, final CommandResult commandResult) {
38-
super(code, commandResult);
39-
}
4036
}

src/main/com/mongodb/MongoClientException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* A base class for exceptions indicating a failure condition within the driver.
21+
*
22+
* @since 2.12
2123
*/
2224
public class MongoClientException extends MongoInternalException {
2325

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.mongodb;
2+
3+
/**
4+
* An exception indicating that a command sent to a MongoDB server returned a failure.
5+
*
6+
* @since 2.13
7+
*/
8+
public class MongoCommandException extends MongoException {
9+
private static final long serialVersionUID = 8160676451944215078L;
10+
private final CommandResult commandResult;
11+
12+
public MongoCommandException(final CommandResult commandResult) {
13+
super(ServerError.getCode(commandResult), commandResult.toString());
14+
this.commandResult = commandResult;
15+
}
16+
17+
/**
18+
* Gets the address of the server that this command failed on.
19+
* @return the server address
20+
*/
21+
public ServerAddress getServerAddress() {
22+
return commandResult.getServerUsed();
23+
}
24+
25+
/**
26+
* Gets the error code associated with the command failure.
27+
*
28+
* @return the error code
29+
*/
30+
public int getErrorCode() {
31+
return getCode();
32+
}
33+
34+
/**
35+
* Gets the error message associated with the command failure.
36+
*
37+
* @return the error message
38+
*/
39+
public String getErrorMessage() {
40+
return commandResult.getErrorMessage();
41+
}
42+
43+
CommandResult getCommandResult() {
44+
return commandResult;
45+
}
46+
}

src/main/com/mongodb/MongoException.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ public static class DuplicateKey extends DuplicateKeyException {
124124
public DuplicateKey(final CommandResult commandResult) {
125125
super(commandResult);
126126
}
127-
128-
DuplicateKey(final int code, final CommandResult commandResult) {
129-
super(code, commandResult);
130-
}
131127
}
132128

133129
/**

src/main/com/mongodb/UnacknowledgedWriteException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
* @see WriteResult
2424
* @see BulkWriteResult
2525
* @since 2.12
26+
* @deprecated Catch {@link java.lang.UnsupportedOperationException instead}
2627
*/
27-
public class UnacknowledgedWriteException extends MongoClientException {
28+
@Deprecated
29+
public class UnacknowledgedWriteException extends UnsupportedOperationException {
2830

2931
private static final long serialVersionUID = 6974332938681213965L;
3032

src/test/com/mongodb/CommandResultTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void testNullErrorCode() throws UnknownHostException {
124124
} catch (CommandFailureException e) {
125125
assertEquals(commandResult, e.getCommandResult());
126126
assertEquals(-5, e.getCode());
127+
assertEquals(-5, e.getErrorCode());
127128
}
128129
}
129130

@@ -141,6 +142,7 @@ public void testCommandFailure() throws UnknownHostException {
141142
assertEquals(new ServerAddress("host1"), e.getServerAddress());
142143
assertEquals(errorMessage, e.getErrorMessage());
143144
assertEquals(5000, e.getCode());
145+
assertEquals(5000, e.getErrorCode());
144146
assertEquals(commandResult, e.getCommandResult());
145147
}
146148
}

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ public void testWriteConcernExceptionOnInsert() throws UnknownHostException {
653653
} catch (WriteConcernException e) {
654654
assertNotNull(e.getServerAddress());
655655
assertNotNull(e.getErrorMessage());
656+
assertEquals(64, e.getCode());
656657
assertNotNull(e.getCommandResult().get("err"));
657658
assertEquals(0, e.getCommandResult().get("n"));
658659
} finally {

0 commit comments

Comments
 (0)