Skip to content

Commit 9c9ab77

Browse files
committed
JAVA-2815: Add error label support to MongoException
1 parent f903e07 commit 9c9ab77

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,29 @@
1818

1919
import com.mongodb.lang.Nullable;
2020

21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.Set;
24+
25+
import static com.mongodb.assertions.Assertions.notNull;
26+
2127
/**
2228
* Top level Exception for all Exceptions, server-side or client-side, that come from the driver.
2329
*/
2430
public class MongoException extends RuntimeException {
31+
32+
/**
33+
* An error label indicating that the exception can be treated as a transient transaction error.
34+
*
35+
* @see #hasErrorLabel(String)
36+
* @since 3.8
37+
*/
38+
public static final String TRANSIENT_TRANSACTION_ERROR_LABEL = "TransientTransactionError";
39+
2540
private static final long serialVersionUID = -4415279469780082174L;
2641

2742
private final int code;
43+
private final Set<String> errorLabels = new HashSet<String>();
2844

2945
/**
3046
* Static helper to create or cast a MongoException from a throwable
@@ -100,4 +116,38 @@ public MongoException(final int code, final String msg, final Throwable t) {
100116
public int getCode() {
101117
return code;
102118
}
119+
120+
/**
121+
* Adds the given error label to the exception.
122+
*
123+
* @param errorLabel the non-null error label to add to the exception
124+
*
125+
* @since 3.8
126+
*/
127+
public void addLabel(final String errorLabel) {
128+
notNull("errorLabel", errorLabel);
129+
errorLabels.add(errorLabel);
130+
}
131+
132+
/**
133+
* Gets the set of error labels associated with this exception.
134+
*
135+
* @return the error labels, which may not be null but may be empty
136+
* @since 3.8
137+
*/
138+
public Set<String> getErrorLabels() {
139+
return Collections.unmodifiableSet(errorLabels);
140+
}
141+
142+
/**
143+
* Return true if the exception is labelled with the given error label, and false otherwise.
144+
*
145+
* @param errorLabel the non-null error label
146+
* @return true if the exception is labelled with the given error label
147+
* @since 3.8
148+
*/
149+
public boolean hasErrorLabel(final String errorLabel) {
150+
notNull("errorLabel", errorLabel);
151+
return errorLabels.contains(errorLabel);
152+
}
103153
}

0 commit comments

Comments
 (0)