Skip to content

Commit 0dbd939

Browse files
committed
Add errorCodeName property to MongoServerException
Also, ensure that MongoQueryException pushes the property down through the constructor JAVA-4565
1 parent ebada8e commit 0dbd939

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MongoCommandException extends MongoServerException {
4646
* @param address the address of the server that generated the response
4747
*/
4848
public MongoCommandException(final BsonDocument response, final ServerAddress address) {
49-
super(extractErrorCode(response),
49+
super(extractErrorCode(response), extractErrorCodeName(response),
5050
format("Command failed with error %s: '%s' on server %s. The full response is %s", extractErrorCodeAndName(response),
5151
extractErrorMessage(response), address, getResponseAsJson(response)), address);
5252
this.response = response;
@@ -70,7 +70,7 @@ public int getErrorCode() {
7070
* @mongodb.server.release 3.4
7171
*/
7272
public String getErrorCodeName() {
73-
return extractErrorCodeName(response);
73+
return super.getErrorCodeName();
7474
}
7575

7676
/**

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb;
1818

19+
import com.mongodb.lang.Nullable;
20+
1921
import static java.lang.String.format;
2022

2123
/**
@@ -41,14 +43,33 @@ public MongoQueryException(final ServerAddress address, final int errorCode, fin
4143
this.errorMessage = errorMessage;
4244
}
4345

46+
/**
47+
* Construct an instance.
48+
*
49+
* @param address the server address
50+
* @param errorCode the error code
51+
* @param errorCodeName the error code name
52+
* @param errorMessage the error message
53+
* @since 4.6
54+
*/
55+
public MongoQueryException(final ServerAddress address, final int errorCode, final @Nullable String errorCodeName,
56+
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;
62+
}
63+
4464
/**
4565
* Construct an instance from a command exception.
4666
*
4767
* @param commandException the command exception
4868
* @since 3.7
4969
*/
5070
public MongoQueryException(final MongoCommandException commandException) {
51-
this(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorMessage());
71+
this(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorCodeName(),
72+
commandException.getErrorMessage());
5273
addLabels(commandException.getErrorLabels());
5374
}
5475

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

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

1717
package com.mongodb;
1818

19+
import com.mongodb.lang.Nullable;
20+
1921
/**
2022
* An exception indicating that some error has been raised by a MongoDB server in response to an operation.
2123
*
@@ -24,6 +26,8 @@
2426
*/
2527
public abstract class MongoServerException extends MongoException {
2628
private static final long serialVersionUID = -5213859742051776206L;
29+
@Nullable
30+
private final String errorCodeName;
2731
private final ServerAddress serverAddress;
2832

2933
/**
@@ -35,6 +39,7 @@ public abstract class MongoServerException extends MongoException {
3539
public MongoServerException(final String message, final ServerAddress serverAddress) {
3640
super(message);
3741
this.serverAddress = serverAddress;
42+
this.errorCodeName = null;
3843
}
3944

4045
/**
@@ -47,6 +52,23 @@ public MongoServerException(final String message, final ServerAddress serverAddr
4752
public MongoServerException(final int code, final String message, final ServerAddress serverAddress) {
4853
super(code, message);
4954
this.serverAddress = serverAddress;
55+
this.errorCodeName = null;
56+
}
57+
58+
/**
59+
* Construct a new instance.
60+
*
61+
* @param code the error code from the server
62+
* @param errorCodeName the error code name from the server
63+
* @param message the message from the server
64+
* @param serverAddress the address of the server
65+
* @since 4.6
66+
*/
67+
public MongoServerException(final int code, final @Nullable String errorCodeName, final String message,
68+
final ServerAddress serverAddress) {
69+
super(code, message);
70+
this.errorCodeName = errorCodeName;
71+
this.serverAddress = serverAddress;
5072
}
5173

5274
/**
@@ -57,4 +79,16 @@ public MongoServerException(final int code, final String message, final ServerAd
5779
public ServerAddress getServerAddress() {
5880
return serverAddress;
5981
}
82+
83+
/**
84+
* Get the error code name, which may be null
85+
*
86+
* @return the error code nam
87+
* @mongodb.server.release 3.4
88+
* @since 4.6
89+
*/
90+
@Nullable
91+
public String getErrorCodeName() {
92+
return errorCodeName;
93+
}
6094
}

0 commit comments

Comments
 (0)