Skip to content

Commit 7ef2da8

Browse files
committed
RUST-765 Remove Deref implementation from Error (C-DEREF)
1 parent 04bd0d1 commit 7ef2da8

File tree

3 files changed

+100
-109
lines changed

3 files changed

+100
-109
lines changed

src/error.rs

Lines changed: 94 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Error {
8787
if self.is_network_error() {
8888
return true;
8989
}
90-
match &self.kind.code() {
90+
match self.code() {
9191
Some(code) => {
9292
RETRYABLE_READ_CODES.contains(&code)
9393
}
@@ -110,7 +110,7 @@ impl Error {
110110
if self.is_network_error() {
111111
return true;
112112
}
113-
match &self.kind.code() {
113+
match &self.code() {
114114
Some(code) => RETRYABLE_WRITE_CODES.contains(&code),
115115
None => false,
116116
}
@@ -193,6 +193,98 @@ impl Error {
193193
pub(crate) fn from_resolve_error(error: trust_dns_resolver::error::ResolveError) -> Self {
194194
ErrorKind::DnsResolveError { message: error.to_string() }.into()
195195
}
196+
197+
pub(crate) fn is_non_timeout_network_error(&self) -> bool {
198+
matches!(self.kind.as_ref(), ErrorKind::Io(ref io_err) if io_err.kind() != std::io::ErrorKind::TimedOut)
199+
}
200+
201+
pub(crate) fn is_network_error(&self) -> bool {
202+
matches!(
203+
self.kind.as_ref(),
204+
ErrorKind::Io(..) | ErrorKind::ConnectionPoolClearedError { .. }
205+
)
206+
}
207+
208+
/// Gets the code from this error for performing SDAM updates, if applicable.
209+
/// Any codes contained in WriteErrors are ignored.
210+
pub(crate) fn code(&self) -> Option<i32> {
211+
match self.kind.as_ref() {
212+
ErrorKind::CommandError(command_error) => {
213+
Some(command_error.code)
214+
},
215+
// According to SDAM spec, write concern error codes MUST also be checked, and writeError codes
216+
// MUST NOT be checked.
217+
ErrorKind::BulkWriteError(BulkWriteFailure { write_concern_error: Some(wc_error), .. }) => {
218+
Some(wc_error.code)
219+
}
220+
ErrorKind::WriteError(WriteFailure::WriteConcernError(wc_error)) => Some(wc_error.code),
221+
_ => None
222+
}
223+
}
224+
225+
/// Gets the server's message for this error, if applicable, for use in testing.
226+
/// If this error is a BulkWriteError, the messages are concatenated.
227+
#[cfg(test)]
228+
pub(crate) fn server_message(&self) -> Option<String> {
229+
match self.kind.as_ref() {
230+
ErrorKind::CommandError(command_error) => {
231+
Some(command_error.message.clone())
232+
},
233+
// since this is used primarily for errorMessageContains assertions in the unified runner, we just
234+
// concatenate all the relevant server messages into one for bulk errors.
235+
ErrorKind::BulkWriteError(BulkWriteFailure { write_concern_error, write_errors }) => {
236+
let mut msg = "".to_string();
237+
if let Some(wc_error) = write_concern_error {
238+
msg.push_str(wc_error.message.as_str());
239+
}
240+
if let Some(write_errors) = write_errors {
241+
for we in write_errors {
242+
msg.push_str(we.message.as_str());
243+
}
244+
}
245+
Some(msg)
246+
}
247+
ErrorKind::WriteError(WriteFailure::WriteConcernError(wc_error)) => Some(wc_error.message.clone()),
248+
ErrorKind::WriteError(WriteFailure::WriteError(write_error)) => Some(write_error.message.clone()),
249+
_ => None
250+
}
251+
}
252+
253+
/// Gets the code name from this error, if applicable.
254+
#[cfg(test)]
255+
pub(crate) fn code_name(&self) -> Option<&str> {
256+
match self.kind.as_ref() {
257+
ErrorKind::CommandError(ref cmd_err) => Some(cmd_err.code_name.as_str()),
258+
ErrorKind::WriteError(ref failure) => match failure {
259+
WriteFailure::WriteConcernError(ref wce) => Some(wce.code_name.as_str()),
260+
WriteFailure::WriteError(ref we) => we.code_name.as_deref(),
261+
},
262+
ErrorKind::BulkWriteError(ref bwe) => bwe
263+
.write_concern_error
264+
.as_ref()
265+
.map(|wce| wce.code_name.as_str()),
266+
_ => None,
267+
}
268+
}
269+
270+
/// If this error corresponds to a "not master" error as per the SDAM spec.
271+
pub(crate) fn is_not_master(&self) -> bool {
272+
self.code().map(|code| NOTMASTER_CODES.contains(&code)).unwrap_or(false)
273+
}
274+
275+
/// If this error corresponds to a "node is recovering" error as per the SDAM spec.
276+
pub(crate) fn is_recovering(&self) -> bool {
277+
self.code()
278+
.map(|code| RECOVERING_CODES.contains(&code))
279+
.unwrap_or(false)
280+
}
281+
282+
/// If this error corresponds to a "node is shutting down" error as per the SDAM spec.
283+
pub(crate) fn is_shutting_down(&self) -> bool {
284+
self.code()
285+
.map(|code| SHUTTING_DOWN_CODES.contains(&code))
286+
.unwrap_or(false)
287+
}
196288
}
197289

198290
impl<E> From<E> for Error
@@ -231,14 +323,6 @@ impl From<std::io::ErrorKind> for ErrorKind {
231323
}
232324
}
233325

234-
impl std::ops::Deref for Error {
235-
type Target = ErrorKind;
236-
237-
fn deref(&self) -> &Self::Target {
238-
&self.kind
239-
}
240-
}
241-
242326
/// The types of errors that can occur.
243327
#[allow(missing_docs)]
244328
#[derive(Clone, Debug, Error)]
@@ -320,99 +404,6 @@ pub enum ErrorKind {
320404
WriteError(WriteFailure),
321405
}
322406

323-
impl ErrorKind {
324-
pub(crate) fn is_non_timeout_network_error(&self) -> bool {
325-
matches!(self, ErrorKind::Io(ref io_err) if io_err.kind() != std::io::ErrorKind::TimedOut)
326-
}
327-
328-
pub(crate) fn is_network_error(&self) -> bool {
329-
matches!(
330-
self,
331-
ErrorKind::Io(..) | ErrorKind::ConnectionPoolClearedError { .. }
332-
)
333-
}
334-
335-
/// Gets the code from this error for performing SDAM updates, if applicable.
336-
/// Any codes contained in WriteErrors are ignored.
337-
pub(crate) fn code(&self) -> Option<i32> {
338-
match self {
339-
ErrorKind::CommandError(command_error) => {
340-
Some(command_error.code)
341-
},
342-
// According to SDAM spec, write concern error codes MUST also be checked, and writeError codes
343-
// MUST NOT be checked.
344-
ErrorKind::BulkWriteError(BulkWriteFailure { write_concern_error: Some(wc_error), .. }) => {
345-
Some(wc_error.code)
346-
}
347-
ErrorKind::WriteError(WriteFailure::WriteConcernError(wc_error)) => Some(wc_error.code),
348-
_ => None
349-
}
350-
}
351-
352-
/// Gets the server's message for this error, if applicable, for use in testing.
353-
/// If this error is a BulkWriteError, the messages are concatenated.
354-
#[cfg(test)]
355-
pub(crate) fn server_message(&self) -> Option<String> {
356-
match self {
357-
ErrorKind::CommandError(command_error) => {
358-
Some(command_error.message.clone())
359-
},
360-
// since this is used primarily for errorMessageContains assertions in the unified runner, we just
361-
// concatenate all the relevant server messages into one for bulk errors.
362-
ErrorKind::BulkWriteError(BulkWriteFailure { write_concern_error, write_errors }) => {
363-
let mut msg = "".to_string();
364-
if let Some(wc_error) = write_concern_error {
365-
msg.push_str(wc_error.message.as_str());
366-
}
367-
if let Some(write_errors) = write_errors {
368-
for we in write_errors {
369-
msg.push_str(we.message.as_str());
370-
}
371-
}
372-
Some(msg)
373-
}
374-
ErrorKind::WriteError(WriteFailure::WriteConcernError(wc_error)) => Some(wc_error.message.clone()),
375-
ErrorKind::WriteError(WriteFailure::WriteError(write_error)) => Some(write_error.message.clone()),
376-
_ => None
377-
}
378-
}
379-
380-
/// Gets the code name from this error, if applicable.
381-
#[cfg(test)]
382-
pub(crate) fn code_name(&self) -> Option<&str> {
383-
match self {
384-
ErrorKind::CommandError(ref cmd_err) => Some(cmd_err.code_name.as_str()),
385-
ErrorKind::WriteError(ref failure) => match failure {
386-
WriteFailure::WriteConcernError(ref wce) => Some(wce.code_name.as_str()),
387-
WriteFailure::WriteError(ref we) => we.code_name.as_deref(),
388-
},
389-
ErrorKind::BulkWriteError(ref bwe) => bwe
390-
.write_concern_error
391-
.as_ref()
392-
.map(|wce| wce.code_name.as_str()),
393-
_ => None,
394-
}
395-
}
396-
397-
/// If this error corresponds to a "not master" error as per the SDAM spec.
398-
pub(crate) fn is_not_master(&self) -> bool {
399-
self.code().map(|code| NOTMASTER_CODES.contains(&code)).unwrap_or(false)
400-
}
401-
402-
/// If this error corresponds to a "node is recovering" error as per the SDAM spec.
403-
pub(crate) fn is_recovering(&self) -> bool {
404-
self.code()
405-
.map(|code| RECOVERING_CODES.contains(&code))
406-
.unwrap_or(false)
407-
}
408-
409-
/// If this error corresponds to a "node is shutting down" error as per the SDAM spec.
410-
pub(crate) fn is_shutting_down(&self) -> bool {
411-
self.code()
412-
.map(|code| SHUTTING_DOWN_CODES.contains(&code))
413-
.unwrap_or(false)
414-
}
415-
}
416407

417408
/// An error that occurred due to a database command failing.
418409
#[derive(Clone, Debug, Deserialize)]

src/test/spec/unified_runner/test_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,19 @@ impl ExpectError {
295295
assert_eq!(is_client_error, !error.is_server_error());
296296
}
297297
if let Some(error_contains) = self.error_contains {
298-
match &error.kind.server_message() {
298+
match &error.server_message() {
299299
Some(msg) => assert!(msg.contains(&error_contains)),
300300
None => panic!("{} should include message field", error),
301301
}
302302
}
303303
if let Some(error_code) = self.error_code {
304-
match &error.kind.code() {
304+
match &error.code() {
305305
Some(code) => assert_eq!(*code, error_code),
306306
None => panic!("{} should include code", error),
307307
}
308308
}
309309
if let Some(error_code_name) = self.error_code_name {
310-
match &error.kind.code_name() {
310+
match &error.code_name() {
311311
Some(name) => assert_eq!(&error_code_name, name),
312312
None => panic!("{} should include code name", error),
313313
}

src/test/spec/v2_runner/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub async fn run_v2_test(test_file: TestFile) {
6363
.await
6464
{
6565
Ok(_) => {}
66-
Err(err) => match err.kind.code() {
66+
Err(err) => match err.code() {
6767
Some(11601) => {}
6868
_ => panic!("{}: killAllSessions failed", test.description),
6969
},
@@ -234,11 +234,11 @@ pub async fn run_v2_test(test_file: TestFile) {
234234
OperationResult::Error(operation_error) => {
235235
let error = result.unwrap_err();
236236
if let Some(error_contains) = operation_error.error_contains {
237-
let message = error.kind.server_message().unwrap();
237+
let message = error.server_message().unwrap();
238238
assert!(message.contains(&error_contains));
239239
}
240240
if let Some(error_code_name) = operation_error.error_code_name {
241-
let code_name = error.kind.code_name().unwrap();
241+
let code_name = error.code_name().unwrap();
242242
assert_eq!(error_code_name, code_name);
243243
}
244244
if let Some(error_labels_contain) = operation_error.error_labels_contain {

0 commit comments

Comments
 (0)