Skip to content

Commit 897986f

Browse files
authored
RUST-392 Suppress "ns not found" errors (#176)
1 parent 5ca992f commit 897986f

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

src/client/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl Client {
209209
}
210210
}
211211

212-
Err(error)
212+
op.handle_error(error)
213213
}
214214
Ok(response) => {
215215
self.emit_command_event(|handler| {

src/error.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ impl Error {
5858
let error: Error = other_error_kind.into();
5959
std::io::Error::new(std::io::ErrorKind::Other, Box::new(error))
6060
}
61-
Err(e) => std::io::Error::new(std::io::ErrorKind::Other, Box::new(Error { kind: e }))
61+
Err(e) => std::io::Error::new(std::io::ErrorKind::Other, Box::new(Error { kind: e })),
62+
}
63+
}
64+
65+
/// Whether this error is an "ns not found" error or not.
66+
pub(crate) fn is_ns_not_found(&self) -> bool {
67+
match self.kind.as_ref() {
68+
ErrorKind::CommandError(err) if err.code == 26 => true,
69+
_ => false,
6270
}
6371
}
6472
}

src/operation/drop_collection/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bson::doc;
55

66
use crate::{
77
cmap::{Command, CommandResponse, StreamDescription},
8-
error::Result,
8+
error::{Error, Result},
99
operation::{append_options, Operation, WriteConcernOnlyBody},
1010
options::{DropCollectionOptions, WriteConcern},
1111
Namespace,
@@ -56,6 +56,14 @@ impl Operation for DropCollection {
5656
response.body::<WriteConcernOnlyBody>()?.validate()
5757
}
5858

59+
fn handle_error(&self, error: Error) -> Result<Self::O> {
60+
if error.is_ns_not_found() {
61+
Ok(())
62+
} else {
63+
Err(error)
64+
}
65+
}
66+
5967
fn write_concern(&self) -> Option<&WriteConcern> {
6068
self.options
6169
.as_ref()

src/operation/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ use serde::{Deserialize, Serialize};
2121

2222
use crate::{
2323
cmap::{Command, CommandResponse, StreamDescription},
24-
error::{BulkWriteError, BulkWriteFailure, ErrorKind, Result, WriteConcernError, WriteFailure},
24+
error::{
25+
BulkWriteError,
26+
BulkWriteFailure,
27+
Error,
28+
ErrorKind,
29+
Result,
30+
WriteConcernError,
31+
WriteFailure,
32+
},
2533
options::WriteConcern,
2634
selection_criteria::SelectionCriteria,
2735
Namespace,
@@ -57,6 +65,12 @@ pub(crate) trait Operation {
5765
/// Interprets the server response to the command.
5866
fn handle_response(&self, response: CommandResponse) -> Result<Self::O>;
5967

68+
/// Interpret an error encountered while sending the built command to the server, potentially
69+
/// recovering.
70+
fn handle_error(&self, error: Error) -> Result<Self::O> {
71+
Err(error)
72+
}
73+
6074
/// Criteria to use for selecting the server that this operation will be executed on.
6175
fn selection_criteria(&self) -> Option<&SelectionCriteria> {
6276
None

src/test/coll.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,15 @@ async fn allow_disk_use_test(options: FindOptions, expected_value: Option<bool>)
533533
assert_eq!(allow_disk_use, expected_value);
534534
assert_eq!(iter.count(), 0);
535535
}
536+
537+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
538+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
539+
#[function_name::named]
540+
async fn ns_not_found_suppression() {
541+
let _guard = LOCK.run_concurrently().await;
542+
543+
let client = TestClient::new().await;
544+
let coll = client.get_coll(function_name!(), function_name!());
545+
coll.drop(None).await.expect("drop should not fail");
546+
coll.drop(None).await.expect("drop should not fail");
547+
}

0 commit comments

Comments
 (0)