Skip to content

Commit 087576b

Browse files
committed
Update MongoQueryException to extend MongoCommandException
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
1 parent db912cd commit 087576b

File tree

8 files changed

+62
-54
lines changed

8 files changed

+62
-54
lines changed

driver-core/src/main/com/mongodb/MongoCommandException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public String getErrorMessage() {
8383
}
8484

8585
/**
86-
* For internal use only.
86+
* Gets the full server response document describing the error.
8787
*
8888
* @return the full response to the command failure.
89+
* @since 4.8
8990
*/
9091
public BsonDocument getResponse() {
9192
return response;

driver-core/src/main/com/mongodb/MongoCursorNotFoundException.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb;
1818

19+
import org.bson.BsonDocument;
20+
1921
/**
2022
* Subclass of {@link MongoException} representing a cursor-not-found exception.
2123
*
@@ -27,18 +29,31 @@ public class MongoCursorNotFoundException extends MongoQueryException {
2729
private static final long serialVersionUID = -4415279469780082174L;
2830

2931
private final long cursorId;
30-
private final ServerAddress serverAddress;
32+
33+
/**
34+
* Construct an instance.
35+
*
36+
* @param cursorId cursor identifier
37+
* @param response the server response document
38+
* @param serverAddress the server address
39+
* @since 4.8
40+
*/
41+
public MongoCursorNotFoundException(final long cursorId, final BsonDocument response, final ServerAddress serverAddress) {
42+
super(response, serverAddress);
43+
this.cursorId = cursorId;
44+
}
3145

3246
/**
3347
* Construct a new instance.
3448
*
3549
* @param cursorId cursor identifier
3650
* @param serverAddress server address
51+
* @deprecated Prefer {@link #MongoCursorNotFoundException(long, BsonDocument, ServerAddress)}
3752
*/
53+
@Deprecated
3854
public MongoCursorNotFoundException(final long cursorId, final ServerAddress serverAddress) {
3955
super(serverAddress, -5, "Cursor " + cursorId + " not found on server " + serverAddress);
4056
this.cursorId = cursorId;
41-
this.serverAddress = serverAddress;
4257
}
4358

4459
/**
@@ -49,13 +64,4 @@ public MongoCursorNotFoundException(final long cursorId, final ServerAddress ser
4964
public long getCursorId() {
5065
return cursorId;
5166
}
52-
53-
/**
54-
* The server address where the cursor is.
55-
*
56-
* @return the ServerAddress representing the server the cursor was on.
57-
*/
58-
public ServerAddress getServerAddress() {
59-
return serverAddress;
60-
}
6167
}

driver-core/src/main/com/mongodb/MongoQueryException.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,41 @@
1717
package com.mongodb;
1818

1919
import com.mongodb.lang.Nullable;
20-
21-
import static java.lang.String.format;
20+
import org.bson.BsonDocument;
21+
import org.bson.BsonInt32;
22+
import org.bson.BsonString;
2223

2324
/**
2425
* An exception indicating that a query operation failed on the server.
2526
*
2627
* @since 3.0
2728
* @serial exclude
2829
*/
29-
public class MongoQueryException extends MongoServerException {
30+
public class MongoQueryException extends MongoCommandException {
3031
private static final long serialVersionUID = -5113350133297015801L;
31-
private final String errorMessage;
32+
33+
/**
34+
* Construct an instance.
35+
*
36+
* @param response the server response document
37+
* @param serverAddress the server address
38+
* @since 4.8
39+
*/
40+
public MongoQueryException(final BsonDocument response, final ServerAddress serverAddress) {
41+
super(response, serverAddress);
42+
}
3243

3344
/**
3445
* Construct an instance.
3546
*
3647
* @param address the server address
3748
* @param errorCode the error code
3849
* @param errorMessage the error message
50+
* @deprecated Prefer {@link #MongoQueryException(BsonDocument, ServerAddress)}
3951
*/
52+
@Deprecated
4053
public MongoQueryException(final ServerAddress address, final int errorCode, final String errorMessage) {
41-
super(errorCode, format("Query failed with error code %d and error message '%s' on server %s", errorCode, errorMessage, address),
42-
address);
43-
this.errorMessage = errorMessage;
54+
this(manufactureResponse(errorCode, null, errorMessage), address);
4455
}
4556

4657
/**
@@ -51,43 +62,33 @@ public MongoQueryException(final ServerAddress address, final int errorCode, fin
5162
* @param errorCodeName the error code name
5263
* @param errorMessage the error message
5364
* @since 4.6
65+
* @deprecated Prefer {@link #MongoQueryException(BsonDocument, ServerAddress)}
5466
*/
55-
public MongoQueryException(final ServerAddress address, final int errorCode, final @Nullable String errorCodeName,
67+
@Deprecated
68+
public MongoQueryException(final ServerAddress address, final int errorCode, @Nullable final String errorCodeName,
5669
final String errorMessage) {
57-
super(errorCode, errorCodeName,
58-
format("Query failed with error code %d with name '%s' and error message '%s' on server %s", errorCode, errorCodeName,
59-
errorMessage, address),
60-
address);
61-
this.errorMessage = errorMessage;
70+
this(manufactureResponse(errorCode, errorCodeName, errorMessage), address);
6271
}
6372

6473
/**
6574
* Construct an instance from a command exception.
6675
*
6776
* @param commandException the command exception
6877
* @since 3.7
78+
* @deprecated Prefer {@link #MongoQueryException(BsonDocument, ServerAddress)}
6979
*/
80+
@Deprecated
7081
public MongoQueryException(final MongoCommandException commandException) {
71-
this(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorCodeName(),
72-
commandException.getErrorMessage());
73-
addLabels(commandException.getErrorLabels());
82+
this(commandException.getResponse(), commandException.getServerAddress());
7483
}
7584

76-
/**
77-
* Gets the error code for this query failure.
78-
*
79-
* @return the error code
80-
*/
81-
public int getErrorCode() {
82-
return getCode();
83-
}
84-
85-
/**
86-
* Gets the error message for this query failure.
87-
*
88-
* @return the error message
89-
*/
90-
public String getErrorMessage() {
91-
return errorMessage;
85+
private static BsonDocument manufactureResponse(final int errorCode, @Nullable final String errorCodeName, final String errorMessage) {
86+
BsonDocument response = new BsonDocument("ok", new BsonInt32(1))
87+
.append("code", new BsonInt32(errorCode))
88+
.append("errmsg", new BsonString(errorMessage));
89+
if (errorCodeName != null) {
90+
response.append("codeName", new BsonString(errorCodeName));
91+
}
92+
return response;
9293
}
9394
}

driver-core/src/main/com/mongodb/internal/connection/ProtocolHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static MongoException getQueryFailureException(final BsonDocument errorDocument,
211211
if (specialException != null) {
212212
return specialException;
213213
}
214-
return new MongoQueryException(serverAddress, getErrorCode(errorDocument), getErrorMessage(errorDocument, "$err"));
214+
return new MongoQueryException(errorDocument, serverAddress);
215215
}
216216

217217
static MessageSettings getMessageSettings(final ConnectionDescription connectionDescription) {

driver-core/src/main/com/mongodb/internal/operation/FindOperation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public BatchCursor<T> execute(final ReadBinding binding) {
333333
getCommandCreator(binding.getSessionContext()), CommandResultDocumentCodec.create(decoder, FIRST_BATCH),
334334
transformer(), connection);
335335
} catch (MongoCommandException e) {
336-
throw new MongoQueryException(e);
336+
throw new MongoQueryException(e.getResponse(), e.getServerAddress());
337337
}
338338
});
339339
});
@@ -370,9 +370,8 @@ public void onResult(final T result, final Throwable t) {
370370
if (t != null) {
371371
if (t instanceof MongoCommandException) {
372372
MongoCommandException commandException = (MongoCommandException) t;
373-
callback.onResult(result, new MongoQueryException(commandException.getServerAddress(),
374-
commandException.getErrorCode(),
375-
commandException.getErrorMessage()));
373+
callback.onResult(result,
374+
new MongoQueryException(commandException.getResponse(), commandException.getServerAddress()));
376375
} else {
377376
callback.onResult(result, t);
378377
}

driver-core/src/main/com/mongodb/internal/operation/QueryHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
final class QueryHelper {
2525
static MongoQueryException translateCommandException(final MongoCommandException commandException, final ServerCursor cursor) {
2626
if (commandException.getErrorCode() == 43) {
27-
return new MongoCursorNotFoundException(cursor.getId(), cursor.getAddress());
27+
return new MongoCursorNotFoundException(cursor.getId(), commandException.getResponse(), cursor.getAddress());
2828
} else {
29-
return new MongoQueryException(commandException);
29+
return new MongoQueryException(commandException.getResponse(), commandException.getServerAddress());
3030
}
3131
}
3232

driver-core/src/test/functional/com/mongodb/internal/operation/QueryOperationHelper.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ class QueryOperationHelper {
8585
new BsonDocumentCodec(), new NoOpSessionContext(), getServerApi(), IgnorableRequestContext.INSTANCE)
8686
} catch (MongoCommandException e) {
8787
if (e.getErrorCode() == 43) {
88-
throw new MongoCursorNotFoundException(serverCursor.getId(), serverCursor.getAddress())
88+
throw new MongoCursorNotFoundException(serverCursor.getId(), e.getResponse(), serverCursor.getAddress())
8989
} else {
90-
throw new MongoQueryException(e)
90+
throw new MongoQueryException(e.getResponse(), e.getServerAddress());
9191
}
9292
}
9393
}

driver-core/src/test/unit/com/mongodb/internal/operation/ChangeStreamBatchCursorHelperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public void testIsResumableError() {
5555

5656
() -> assertTrue(isResumableError(new MongoNotPrimaryException(new BsonDocument(), new ServerAddress()),
5757
FOUR_DOT_FOUR_WIRE_VERSION)),
58-
() -> assertTrue(isResumableError(new MongoCursorNotFoundException(1L, new ServerAddress()), FOUR_DOT_FOUR_WIRE_VERSION)),
58+
() -> assertTrue(isResumableError(new MongoCursorNotFoundException(1L, new BsonDocument("ok", new BsonInt32(0))
59+
.append("code", new BsonInt32(43)), new ServerAddress()), FOUR_DOT_FOUR_WIRE_VERSION)),
5960
() -> assertTrue(isResumableError(new MongoSocketException("", new ServerAddress()), FOUR_DOT_FOUR_WIRE_VERSION)),
6061
() -> assertTrue(isResumableError(new MongoSocketReadTimeoutException("", new ServerAddress(), new IOException()),
6162
FOUR_DOT_FOUR_WIRE_VERSION)),

0 commit comments

Comments
 (0)