Skip to content

Commit

Permalink
Update MongoQueryException to extend MongoCommandException
Browse files Browse the repository at this point in the history
The MongoQueryException class currently extends MongoServerException,
the reason being that prior to MongoDB 3.2 the find helper was implemented
using the OP_QUERY wire protocol message, which is not a command.  But now
that 3.6 is the minimum required server version, the find helper is always
implemented via the find command.  So MongoQueryException can now extend
MongoCommandException and include the full command response document.

JAVA-4676
  • Loading branch information
jyemin committed Nov 9, 2022
1 parent db912cd commit 087576b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 54 deletions.
3 changes: 2 additions & 1 deletion driver-core/src/main/com/mongodb/MongoCommandException.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public String getErrorMessage() {
}

/**
* For internal use only.
* Gets the full server response document describing the error.
*
* @return the full response to the command failure.
* @since 4.8
*/
public BsonDocument getResponse() {
return response;
Expand Down
28 changes: 17 additions & 11 deletions driver-core/src/main/com/mongodb/MongoCursorNotFoundException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.mongodb;

import org.bson.BsonDocument;

/**
* Subclass of {@link MongoException} representing a cursor-not-found exception.
*
Expand All @@ -27,18 +29,31 @@ public class MongoCursorNotFoundException extends MongoQueryException {
private static final long serialVersionUID = -4415279469780082174L;

private final long cursorId;
private final ServerAddress serverAddress;

/**
* Construct an instance.
*
* @param cursorId cursor identifier
* @param response the server response document
* @param serverAddress the server address
* @since 4.8
*/
public MongoCursorNotFoundException(final long cursorId, final BsonDocument response, final ServerAddress serverAddress) {
super(response, serverAddress);
this.cursorId = cursorId;
}

/**
* Construct a new instance.
*
* @param cursorId cursor identifier
* @param serverAddress server address
* @deprecated Prefer {@link #MongoCursorNotFoundException(long, BsonDocument, ServerAddress)}
*/
@Deprecated
public MongoCursorNotFoundException(final long cursorId, final ServerAddress serverAddress) {
super(serverAddress, -5, "Cursor " + cursorId + " not found on server " + serverAddress);
this.cursorId = cursorId;
this.serverAddress = serverAddress;
}

/**
Expand All @@ -49,13 +64,4 @@ public MongoCursorNotFoundException(final long cursorId, final ServerAddress ser
public long getCursorId() {
return cursorId;
}

/**
* The server address where the cursor is.
*
* @return the ServerAddress representing the server the cursor was on.
*/
public ServerAddress getServerAddress() {
return serverAddress;
}
}
65 changes: 33 additions & 32 deletions driver-core/src/main/com/mongodb/MongoQueryException.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,41 @@
package com.mongodb;

import com.mongodb.lang.Nullable;

import static java.lang.String.format;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonString;

/**
* An exception indicating that a query operation failed on the server.
*
* @since 3.0
* @serial exclude
*/
public class MongoQueryException extends MongoServerException {
public class MongoQueryException extends MongoCommandException {
private static final long serialVersionUID = -5113350133297015801L;
private final String errorMessage;

/**
* Construct an instance.
*
* @param response the server response document
* @param serverAddress the server address
* @since 4.8
*/
public MongoQueryException(final BsonDocument response, final ServerAddress serverAddress) {
super(response, serverAddress);
}

/**
* Construct an instance.
*
* @param address the server address
* @param errorCode the error code
* @param errorMessage the error message
* @deprecated Prefer {@link #MongoQueryException(BsonDocument, ServerAddress)}
*/
@Deprecated
public MongoQueryException(final ServerAddress address, final int errorCode, final String errorMessage) {
super(errorCode, format("Query failed with error code %d and error message '%s' on server %s", errorCode, errorMessage, address),
address);
this.errorMessage = errorMessage;
this(manufactureResponse(errorCode, null, errorMessage), address);
}

/**
Expand All @@ -51,43 +62,33 @@ public MongoQueryException(final ServerAddress address, final int errorCode, fin
* @param errorCodeName the error code name
* @param errorMessage the error message
* @since 4.6
* @deprecated Prefer {@link #MongoQueryException(BsonDocument, ServerAddress)}
*/
public MongoQueryException(final ServerAddress address, final int errorCode, final @Nullable String errorCodeName,
@Deprecated
public MongoQueryException(final ServerAddress address, final int errorCode, @Nullable final String errorCodeName,
final String errorMessage) {
super(errorCode, errorCodeName,
format("Query failed with error code %d with name '%s' and error message '%s' on server %s", errorCode, errorCodeName,
errorMessage, address),
address);
this.errorMessage = errorMessage;
this(manufactureResponse(errorCode, errorCodeName, errorMessage), address);
}

/**
* Construct an instance from a command exception.
*
* @param commandException the command exception
* @since 3.7
* @deprecated Prefer {@link #MongoQueryException(BsonDocument, ServerAddress)}
*/
@Deprecated
public MongoQueryException(final MongoCommandException commandException) {
this(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorCodeName(),
commandException.getErrorMessage());
addLabels(commandException.getErrorLabels());
this(commandException.getResponse(), commandException.getServerAddress());
}

/**
* Gets the error code for this query failure.
*
* @return the error code
*/
public int getErrorCode() {
return getCode();
}

/**
* Gets the error message for this query failure.
*
* @return the error message
*/
public String getErrorMessage() {
return errorMessage;
private static BsonDocument manufactureResponse(final int errorCode, @Nullable final String errorCodeName, final String errorMessage) {
BsonDocument response = new BsonDocument("ok", new BsonInt32(1))
.append("code", new BsonInt32(errorCode))
.append("errmsg", new BsonString(errorMessage));
if (errorCodeName != null) {
response.append("codeName", new BsonString(errorCodeName));
}
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static MongoException getQueryFailureException(final BsonDocument errorDocument,
if (specialException != null) {
return specialException;
}
return new MongoQueryException(serverAddress, getErrorCode(errorDocument), getErrorMessage(errorDocument, "$err"));
return new MongoQueryException(errorDocument, serverAddress);
}

static MessageSettings getMessageSettings(final ConnectionDescription connectionDescription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public BatchCursor<T> execute(final ReadBinding binding) {
getCommandCreator(binding.getSessionContext()), CommandResultDocumentCodec.create(decoder, FIRST_BATCH),
transformer(), connection);
} catch (MongoCommandException e) {
throw new MongoQueryException(e);
throw new MongoQueryException(e.getResponse(), e.getServerAddress());
}
});
});
Expand Down Expand Up @@ -370,9 +370,8 @@ public void onResult(final T result, final Throwable t) {
if (t != null) {
if (t instanceof MongoCommandException) {
MongoCommandException commandException = (MongoCommandException) t;
callback.onResult(result, new MongoQueryException(commandException.getServerAddress(),
commandException.getErrorCode(),
commandException.getErrorMessage()));
callback.onResult(result,
new MongoQueryException(commandException.getResponse(), commandException.getServerAddress()));
} else {
callback.onResult(result, t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
final class QueryHelper {
static MongoQueryException translateCommandException(final MongoCommandException commandException, final ServerCursor cursor) {
if (commandException.getErrorCode() == 43) {
return new MongoCursorNotFoundException(cursor.getId(), cursor.getAddress());
return new MongoCursorNotFoundException(cursor.getId(), commandException.getResponse(), cursor.getAddress());
} else {
return new MongoQueryException(commandException);
return new MongoQueryException(commandException.getResponse(), commandException.getServerAddress());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ class QueryOperationHelper {
new BsonDocumentCodec(), new NoOpSessionContext(), getServerApi(), IgnorableRequestContext.INSTANCE)
} catch (MongoCommandException e) {
if (e.getErrorCode() == 43) {
throw new MongoCursorNotFoundException(serverCursor.getId(), serverCursor.getAddress())
throw new MongoCursorNotFoundException(serverCursor.getId(), e.getResponse(), serverCursor.getAddress())
} else {
throw new MongoQueryException(e)
throw new MongoQueryException(e.getResponse(), e.getServerAddress());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void testIsResumableError() {

() -> assertTrue(isResumableError(new MongoNotPrimaryException(new BsonDocument(), new ServerAddress()),
FOUR_DOT_FOUR_WIRE_VERSION)),
() -> assertTrue(isResumableError(new MongoCursorNotFoundException(1L, new ServerAddress()), FOUR_DOT_FOUR_WIRE_VERSION)),
() -> assertTrue(isResumableError(new MongoCursorNotFoundException(1L, new BsonDocument("ok", new BsonInt32(0))
.append("code", new BsonInt32(43)), new ServerAddress()), FOUR_DOT_FOUR_WIRE_VERSION)),
() -> assertTrue(isResumableError(new MongoSocketException("", new ServerAddress()), FOUR_DOT_FOUR_WIRE_VERSION)),
() -> assertTrue(isResumableError(new MongoSocketReadTimeoutException("", new ServerAddress(), new IOException()),
FOUR_DOT_FOUR_WIRE_VERSION)),
Expand Down

0 comments on commit 087576b

Please sign in to comment.