Skip to content

Commit b4c8de2

Browse files
author
Trisha Gee
committed
Fixed ClassCastException when commands with custom encoders fail.
1 parent 93fa158 commit b4c8de2

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

driver/src/main/org/mongodb/CommandResult.java

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

1919
import org.mongodb.connection.ServerAddress;
2020

21-
public class CommandResult {
21+
public class CommandResult<T> {
2222
private final Document command;
2323
private final ServerAddress address;
2424
private final Document response;
@@ -31,7 +31,7 @@ public CommandResult(final Document command, final ServerAddress address, final
3131
this.elapsedNanoseconds = elapsedNanoseconds;
3232
}
3333

34-
public CommandResult(final CommandResult baseResult) {
34+
public CommandResult(final CommandResult<T> baseResult) {
3535
this.command = baseResult.command;
3636
this.address = baseResult.address;
3737
this.response = baseResult.response;
@@ -55,7 +55,7 @@ public boolean isOk() {
5555
}
5656

5757
public int getErrorCode() {
58-
Integer errorCode = (Integer) getResponse().get("code");
58+
final Integer errorCode = (Integer) getResponse().get("code");
5959
return (errorCode != null) ? errorCode : -1;
6060
}
6161

driver/src/main/org/mongodb/Document.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ public int hashCode() {
248248
*/
249249
@Override
250250
public String toString() {
251+
//TODO: WARNING - this toString will not work if the Document contains any non-standard types,
252+
// i.e. anything that requires a custom codec, like POJOs or custom CollectibleCodecs for generic Collections
251253
final StringWriter writer = new StringWriter();
252254
final BSONWriter bsonWriter = new JSONWriter(writer, new JSONWriterSettings(JSONMode.Strict));
253255
final Codec<Document> codec = new DocumentCodec(PrimitiveCodecs.createDefault());

driver/src/main/org/mongodb/command/MongoCommandFailureException.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.mongodb.CommandResult;
2020
import org.mongodb.operation.MongoServerException;
2121

22+
import static java.lang.String.format;
23+
2224
/**
2325
* Exception thrown when a command fails.
2426
*/
@@ -28,8 +30,8 @@ public class MongoCommandFailureException extends MongoServerException {
2830
private final CommandResult commandResult;
2931

3032
public MongoCommandFailureException(final CommandResult commandResult) {
31-
super("Command failed with response " + commandResult.getResponse() + " on server " + commandResult.getAddress(),
32-
commandResult.getAddress());
33+
super(format("Command failed with error %s: '%s' on server %s", commandResult.getErrorCode(),
34+
commandResult.getErrorMessage(), commandResult.getAddress()), commandResult.getAddress());
3335
this.commandResult = commandResult;
3436
}
3537

0 commit comments

Comments
 (0)