forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoException.java
More file actions
231 lines (206 loc) · 6.23 KB
/
MongoException.java
File metadata and controls
231 lines (206 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import com.mongodb.lang.Nullable;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static com.mongodb.assertions.Assertions.notNull;
/**
* Top level Exception for all Exceptions, server-side or client-side, that come from the driver.
* @serial exclude
*/
public class MongoException extends RuntimeException {
/**
* An error label indicating that the exception can be treated as a transient transaction error.
*
* @see #hasErrorLabel(String)
* @since 3.8
*/
public static final String TRANSIENT_TRANSACTION_ERROR_LABEL = "TransientTransactionError";
/**
* An error label indicating that the exception can be treated as an unknown transaction commit result.
*
* @see #hasErrorLabel(String)
* @since 3.8
*/
public static final String UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL = "UnknownTransactionCommitResult";
/**
* An error label indicating that the server is overloaded.
*
* @see #hasErrorLabel(String)
* @since 5.7
*/
public static final String SYSTEM_OVERLOADED_ERROR_LABEL = "SystemOverloadedError";
/**
* An error label indicating that the operation is safely retryable.
*
* @see #hasErrorLabel(String)
* @since 5.7
*/
public static final String RETRYABLE_ERROR_LABEL = "RetryableError";
private static final long serialVersionUID = -4415279469780082174L;
private final int code;
private final Set<String> errorLabels = new HashSet<>();
/**
* Static helper to create or cast a MongoException from a throwable
*
* @param t a throwable, which may be null
* @return a MongoException
*/
@Nullable
public static MongoException fromThrowable(@Nullable final Throwable t) {
if (t == null) {
return null;
} else {
return fromThrowableNonNull(t);
}
}
/**
* Static helper to create or cast a MongoException from a throwable
*
* @param t a throwable, which may not be null
* @return a MongoException
* @since 3.7
*/
public static MongoException fromThrowableNonNull(final Throwable t) {
if (t instanceof MongoException) {
return (MongoException) t;
} else {
return new MongoException(t.getMessage(), t);
}
}
/**
* @param msg the message
*/
public MongoException(final String msg) {
super(msg);
code = -3;
}
/**
* @param code the error code
* @param msg the message
*/
public MongoException(final int code, final String msg) {
super(msg);
this.code = code;
}
/**
* @param msg the message
* @param t the throwable cause
*/
public MongoException(@Nullable final String msg, @Nullable final Throwable t) {
super(msg, t);
code = -4;
}
/**
* @param code the error code
* @param msg the message
* @param t the throwable cause
*/
public MongoException(final int code, final String msg, final Throwable t) {
super(msg, t);
this.code = code;
if (t instanceof MongoException) {
addLabels(((MongoException) t).getErrorLabels());
}
}
/**
* @param code the error code
* @param msg the message
* @param response the response
* @since 4.1
*/
public MongoException(final int code, final String msg, final BsonDocument response) {
super(msg);
this.code = code;
addLabels(response.getArray("errorLabels", new BsonArray()));
}
/**
* Gets the exception code
*
* @return the error code.
*/
public int getCode() {
return code;
}
/**
* Adds the given error label to the exception.
*
* @param errorLabel the non-null error label to add to the exception
*
* @since 3.8
*/
public void addLabel(final String errorLabel) {
notNull("errorLabel", errorLabel);
errorLabels.add(errorLabel);
}
/**
* Removes the given error label from the exception.
*
* @param errorLabel the non-null error label to remove from the exception
*
* @since 3.8
*/
public void removeLabel(final String errorLabel) {
notNull("errorLabel", errorLabel);
errorLabels.remove(errorLabel);
}
/**
* Gets the set of error labels associated with this exception.
*
* @return the error labels, which may not be null but may be empty
* @since 3.8
*/
public Set<String> getErrorLabels() {
return Collections.unmodifiableSet(errorLabels);
}
/**
* Return true if the exception is labelled with the given error label, and false otherwise.
*
* @param errorLabel the non-null error label
* @return true if the exception is labelled with the given error label
* @since 3.8
*/
public boolean hasErrorLabel(final String errorLabel) {
notNull("errorLabel", errorLabel);
return errorLabels.contains(errorLabel);
}
/**
* Add labels.
*
* @param labels the labels
*/
protected void addLabels(final BsonArray labels) {
for (final BsonValue errorLabel : labels) {
addLabel(errorLabel.asString().getValue());
}
}
/**
* Add labels.
*
* @param labels the labels
*/
protected void addLabels(final Collection<String> labels) {
for (final String errorLabel : labels) {
addLabel(errorLabel);
}
}
}