|
18 | 18 |
|
19 | 19 | import com.mongodb.lang.Nullable;
|
20 | 20 |
|
| 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 | + |
21 | 27 | /**
|
22 | 28 | * Top level Exception for all Exceptions, server-side or client-side, that come from the driver.
|
23 | 29 | */
|
24 | 30 | 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 | + |
25 | 40 | private static final long serialVersionUID = -4415279469780082174L;
|
26 | 41 |
|
27 | 42 | private final int code;
|
| 43 | + private final Set<String> errorLabels = new HashSet<String>(); |
28 | 44 |
|
29 | 45 | /**
|
30 | 46 | * 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) {
|
100 | 116 | public int getCode() {
|
101 | 117 | return code;
|
102 | 118 | }
|
| 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 | + } |
103 | 153 | }
|
0 commit comments