Skip to content

Commit 6adb8c1

Browse files
RUST-1857 Clean up names for multi-write errors (#1102)
1 parent f5d7c4c commit 6adb8c1

File tree

14 files changed

+107
-110
lines changed

14 files changed

+107
-110
lines changed

src/action/bulk_write.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44

55
use crate::{
66
bson::{Bson, Document},
7-
error::{ClientBulkWriteError, Error, ErrorKind, Result},
7+
error::{BulkWriteError, Error, ErrorKind, Result},
88
operation::bulk_write::BulkWrite as BulkWriteOperation,
99
options::{BulkWriteOptions, WriteConcern, WriteModel},
1010
results::BulkWriteResult,
@@ -162,25 +162,24 @@ impl ExecutionStatus {
162162
// partial result. Otherwise, create a new BulkWriteError with the existing results and
163163
// set its source as the error that just occurred.
164164
Self::Success(current_result) => match *error.kind {
165-
ErrorKind::ClientBulkWrite(ref mut bulk_write_error) => {
165+
ErrorKind::BulkWrite(ref mut bulk_write_error) => {
166166
bulk_write_error.merge_partial_results(current_result);
167167
Self::Error(error)
168168
}
169169
_ => {
170-
let bulk_write_error: Error =
171-
ErrorKind::ClientBulkWrite(ClientBulkWriteError {
172-
write_errors: HashMap::new(),
173-
write_concern_errors: Vec::new(),
174-
partial_result: Some(current_result),
175-
})
176-
.into();
170+
let bulk_write_error: Error = ErrorKind::BulkWrite(BulkWriteError {
171+
write_errors: HashMap::new(),
172+
write_concern_errors: Vec::new(),
173+
partial_result: Some(current_result),
174+
})
175+
.into();
177176
Self::Error(bulk_write_error.with_source(error))
178177
}
179178
},
180179
// If the new error is a BulkWriteError, merge its contents with the existing error.
181180
// Otherwise, set the new error as the existing error's source.
182181
Self::Error(mut current_error) => match *error.kind {
183-
ErrorKind::ClientBulkWrite(bulk_write_error) => {
182+
ErrorKind::BulkWrite(bulk_write_error) => {
184183
let current_bulk_write_error =
185184
Self::get_current_bulk_write_error(&mut current_error);
186185
current_bulk_write_error.merge(bulk_write_error);
@@ -195,9 +194,9 @@ impl ExecutionStatus {
195194
/// Gets a BulkWriteError from a given Error. This method should only be called when adding a
196195
/// new result or error to the existing state, as it requires that the given Error's kind is
197196
/// ClientBulkWrite.
198-
fn get_current_bulk_write_error(error: &mut Error) -> &mut ClientBulkWriteError {
197+
fn get_current_bulk_write_error(error: &mut Error) -> &mut BulkWriteError {
199198
match *error.kind {
200-
ErrorKind::ClientBulkWrite(ref mut bulk_write_error) => bulk_write_error,
199+
ErrorKind::BulkWrite(ref mut bulk_write_error) => bulk_write_error,
201200
_ => unreachable!(),
202201
}
203202
}
@@ -208,7 +207,7 @@ impl ExecutionStatus {
208207
match self {
209208
Self::Error(ref error) => {
210209
match *error.kind {
211-
ErrorKind::ClientBulkWrite(ref bulk_write_error) => {
210+
ErrorKind::BulkWrite(ref bulk_write_error) => {
212211
// A top-level error is always fatal.
213212
let top_level_error_occurred = error.source.is_some();
214213
// A write error occurring during an ordered bulk write is fatal.

src/action/insert_many.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::Serialize;
55

66
use crate::{
77
coll::options::InsertManyOptions,
8-
error::{BulkWriteError, BulkWriteFailure, Error, ErrorKind, Result},
8+
error::{Error, ErrorKind, IndexedWriteError, InsertManyError, Result},
99
operation::Insert as Op,
1010
options::WriteConcern,
1111
results::InsertManyResult,
@@ -105,7 +105,7 @@ impl<'a> Action for InsertMany<'a> {
105105
.unwrap_or(true);
106106
let encrypted = self.coll.client().should_auto_encrypt().await;
107107

108-
let mut cumulative_failure: Option<BulkWriteFailure> = None;
108+
let mut cumulative_failure: Option<InsertManyError> = None;
109109
let mut error_labels: HashSet<String> = Default::default();
110110
let mut cumulative_result: Option<InsertManyResult> = None;
111111

@@ -137,7 +137,7 @@ impl<'a> Action for InsertMany<'a> {
137137
Err(e) => {
138138
let labels = e.labels().clone();
139139
match *e.kind {
140-
ErrorKind::BulkWrite(bw) => {
140+
ErrorKind::InsertMany(bw) => {
141141
// for ordered inserts this size will be incorrect, but knowing the
142142
// batch size isn't needed for ordered
143143
// failures since we return immediately from
@@ -146,15 +146,15 @@ impl<'a> Action for InsertMany<'a> {
146146
+ bw.write_errors.as_ref().map(|we| we.len()).unwrap_or(0);
147147

148148
let failure_ref =
149-
cumulative_failure.get_or_insert_with(BulkWriteFailure::new);
149+
cumulative_failure.get_or_insert_with(InsertManyError::new);
150150
if let Some(write_errors) = bw.write_errors {
151151
for err in write_errors {
152152
let index = n_attempted + err.index;
153153

154154
failure_ref
155155
.write_errors
156156
.get_or_insert_with(Default::default)
157-
.push(BulkWriteError { index, ..err });
157+
.push(IndexedWriteError { index, ..err });
158158
}
159159
}
160160

@@ -169,7 +169,7 @@ impl<'a> Action for InsertMany<'a> {
169169
// above.
170170
if let Some(failure) = cumulative_failure {
171171
return Err(Error::new(
172-
ErrorKind::BulkWrite(failure),
172+
ErrorKind::InsertMany(failure),
173173
Some(error_labels),
174174
));
175175
}
@@ -184,7 +184,7 @@ impl<'a> Action for InsertMany<'a> {
184184

185185
match cumulative_failure {
186186
Some(failure) => Err(Error::new(
187-
ErrorKind::BulkWrite(failure),
187+
ErrorKind::InsertMany(failure),
188188
Some(error_labels),
189189
)),
190190
None => Ok(cumulative_result.unwrap_or_else(InsertManyResult::new)),

src/action/insert_one.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::Serialize;
55

66
use crate::{
77
coll::options::{InsertManyOptions, InsertOneOptions},
8-
error::{convert_bulk_errors, Result},
8+
error::{convert_insert_many_error, Result},
99
operation::Insert as Op,
1010
options::WriteConcern,
1111
results::InsertOneResult,
@@ -100,6 +100,6 @@ impl<'a> Action for InsertOne<'a> {
100100
.execute_operation(insert, self.session)
101101
.await
102102
.map(InsertOneResult::from_insert_many_result)
103-
.map_err(convert_bulk_errors)
103+
.map_err(convert_insert_many_error)
104104
}
105105
}

src/error.rs

+47-44
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
sdam::{ServerType, TopologyVersion},
1919
};
2020

21-
pub use bulk_write::BulkWriteError as ClientBulkWriteError;
21+
pub use bulk_write::BulkWriteError;
2222

2323
const RECOVERING_CODES: [i32; 5] = [11600, 11602, 13436, 189, 91];
2424
const NOTWRITABLEPRIMARY_CODES: [i32; 3] = [10107, 13435, 10058];
@@ -195,8 +195,8 @@ impl Error {
195195
fn is_write_concern_error(&self) -> bool {
196196
match *self.kind {
197197
ErrorKind::Write(WriteFailure::WriteConcernError(_)) => true,
198-
ErrorKind::BulkWrite(ref bulk_write_error)
199-
if bulk_write_error.write_concern_error.is_some() =>
198+
ErrorKind::InsertMany(ref insert_many_error)
199+
if insert_many_error.write_concern_error.is_some() =>
200200
{
201201
true
202202
}
@@ -249,7 +249,7 @@ impl Error {
249249
matches!(
250250
self.kind.as_ref(),
251251
ErrorKind::Authentication { .. }
252-
| ErrorKind::BulkWrite(_)
252+
| ErrorKind::InsertMany(_)
253253
| ErrorKind::Command(_)
254254
| ErrorKind::Write(_)
255255
)
@@ -308,7 +308,7 @@ impl Error {
308308
ErrorKind::Command(command_error) => Some(command_error.code),
309309
// According to SDAM spec, write concern error codes MUST also be checked, and
310310
// writeError codes MUST NOT be checked.
311-
ErrorKind::BulkWrite(BulkWriteFailure {
311+
ErrorKind::InsertMany(InsertManyError {
312312
write_concern_error: Some(wc_error),
313313
..
314314
}) => Some(wc_error.code),
@@ -323,7 +323,7 @@ impl Error {
323323
pub(crate) fn code(&self) -> Option<i32> {
324324
match self.kind.as_ref() {
325325
ErrorKind::Command(command_error) => Some(command_error.code),
326-
ErrorKind::BulkWrite(BulkWriteFailure {
326+
ErrorKind::InsertMany(InsertManyError {
327327
write_concern_error: Some(wc_error),
328328
..
329329
}) => Some(wc_error.code),
@@ -334,15 +334,15 @@ impl Error {
334334
}
335335

336336
/// Gets the message for this error, if applicable, for use in testing.
337-
/// If this error is a BulkWriteError, the messages are concatenated.
337+
/// If this error is an InsertManyError, the messages are concatenated.
338338
#[cfg(test)]
339339
pub(crate) fn message(&self) -> Option<String> {
340340
match self.kind.as_ref() {
341341
ErrorKind::Command(command_error) => Some(command_error.message.clone()),
342342
// since this is used primarily for errorMessageContains assertions in the unified
343343
// runner, we just concatenate all the relevant server messages into one for
344-
// bulk errors.
345-
ErrorKind::BulkWrite(BulkWriteFailure {
344+
// insert many errors.
345+
ErrorKind::InsertMany(InsertManyError {
346346
write_concern_error,
347347
write_errors,
348348
inserted_ids: _,
@@ -382,7 +382,7 @@ impl Error {
382382
WriteFailure::WriteConcernError(ref wce) => Some(wce.code_name.as_str()),
383383
WriteFailure::WriteError(ref we) => we.code_name.as_deref(),
384384
},
385-
ErrorKind::BulkWrite(ref bwe) => bwe
385+
ErrorKind::InsertMany(ref bwe) => bwe
386386
.write_concern_error
387387
.as_ref()
388388
.map(|wce| wce.code_name.as_str()),
@@ -481,21 +481,21 @@ impl Error {
481481
// This is intentionally written without a catch-all branch so that if new error
482482
// kinds are added we remember to reason about whether they need to be redacted.
483483
match *self.kind {
484-
ErrorKind::BulkWrite(ref mut bwe) => {
485-
if let Some(ref mut wes) = bwe.write_errors {
484+
ErrorKind::InsertMany(ref mut insert_many_error) => {
485+
if let Some(ref mut wes) = insert_many_error.write_errors {
486486
for we in wes {
487487
we.redact();
488488
}
489489
}
490-
if let Some(ref mut wce) = bwe.write_concern_error {
490+
if let Some(ref mut wce) = insert_many_error.write_concern_error {
491491
wce.redact();
492492
}
493493
}
494-
ErrorKind::ClientBulkWrite(ref mut client_bulk_write_error) => {
495-
for write_concern_error in client_bulk_write_error.write_concern_errors.iter_mut() {
494+
ErrorKind::BulkWrite(ref mut bulk_write_error) => {
495+
for write_concern_error in bulk_write_error.write_concern_errors.iter_mut() {
496496
write_concern_error.redact();
497497
}
498-
for (_, write_error) in client_bulk_write_error.write_errors.iter_mut() {
498+
for (_, write_error) in bulk_write_error.write_errors.iter_mut() {
499499
write_error.redact();
500500
}
501501
}
@@ -612,12 +612,13 @@ pub enum ErrorKind {
612612
#[error("{0}")]
613613
BsonSerialization(crate::bson::ser::Error),
614614

615-
/// An error occurred when trying to execute a write operation consisting of multiple writes.
616-
#[error("An error occurred when trying to execute a write operation: {0:?}")]
617-
BulkWrite(BulkWriteFailure),
615+
/// An error occurred when trying to execute an [`insert_many`](crate::Collection::insert_many)
616+
/// operation.
617+
#[error("An error occurred when trying to execute an insert_many operation: {0:?}")]
618+
InsertMany(InsertManyError),
618619

619620
#[error("An error occurred when executing Client::bulk_write: {0:?}")]
620-
ClientBulkWrite(ClientBulkWriteError),
621+
BulkWrite(BulkWriteError),
621622

622623
/// The server returned an error to an attempted operation.
623624
#[error("Command failed: {0}")]
@@ -706,7 +707,7 @@ impl ErrorKind {
706707
// TODO CLOUDP-105256 Remove this when Atlas Proxy error label behavior is fixed.
707708
fn get_write_concern_error(&self) -> Option<&WriteConcernError> {
708709
match self {
709-
ErrorKind::BulkWrite(BulkWriteFailure {
710+
ErrorKind::InsertMany(InsertManyError {
710711
write_concern_error,
711712
..
712713
}) => write_concern_error.as_ref(),
@@ -825,11 +826,11 @@ impl WriteError {
825826
}
826827
}
827828

828-
/// An error that occurred during a write operation consisting of multiple writes that wasn't due to
829-
/// being unable to satisfy a write concern.
829+
/// An individual write error that occurred during an
830+
/// [`insert_many`](crate::Collection::insert_many) operation.
830831
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
831832
#[non_exhaustive]
832-
pub struct BulkWriteError {
833+
pub struct IndexedWriteError {
833834
/// Index into the list of operations that this error corresponds to.
834835
#[serde(default)]
835836
pub index: usize,
@@ -854,22 +855,23 @@ pub struct BulkWriteError {
854855
pub details: Option<Document>,
855856
}
856857

857-
impl BulkWriteError {
858-
// If any new fields are added to BulkWriteError, this implementation must be updated to redact
858+
impl IndexedWriteError {
859+
// If any new fields are added to InsertError, this implementation must be updated to redact
859860
// them per the CLAM spec.
860861
fn redact(&mut self) {
861862
self.message = "REDACTED".to_string();
862863
self.details = None;
863864
}
864865
}
865866

866-
/// The set of errors that occurred during a write operation.
867+
/// The set of errors that occurred during a call to
868+
/// [`insert_many`](crate::Collection::insert_many).
867869
#[derive(Clone, Debug, Serialize, Deserialize)]
868870
#[serde(rename_all = "camelCase")]
869871
#[non_exhaustive]
870-
pub struct BulkWriteFailure {
872+
pub struct InsertManyError {
871873
/// The error(s) that occurred on account of a non write concern failure.
872-
pub write_errors: Option<Vec<BulkWriteError>>,
874+
pub write_errors: Option<Vec<IndexedWriteError>>,
873875

874876
/// The error that occurred on account of write concern failure.
875877
pub write_concern_error: Option<WriteConcernError>,
@@ -878,9 +880,9 @@ pub struct BulkWriteFailure {
878880
pub(crate) inserted_ids: HashMap<usize, Bson>,
879881
}
880882

881-
impl BulkWriteFailure {
883+
impl InsertManyError {
882884
pub(crate) fn new() -> Self {
883-
BulkWriteFailure {
885+
InsertManyError {
884886
write_errors: None,
885887
write_concern_error: None,
886888
inserted_ids: Default::default(),
@@ -901,13 +903,13 @@ pub enum WriteFailure {
901903
}
902904

903905
impl WriteFailure {
904-
fn from_bulk_failure(bulk: BulkWriteFailure) -> Result<Self> {
905-
if let Some(bulk_write_error) = bulk.write_errors.and_then(|es| es.into_iter().next()) {
906+
fn from_insert_many_error(bulk: InsertManyError) -> Result<Self> {
907+
if let Some(insert_error) = bulk.write_errors.and_then(|es| es.into_iter().next()) {
906908
let write_error = WriteError {
907-
code: bulk_write_error.code,
908-
code_name: bulk_write_error.code_name,
909-
message: bulk_write_error.message,
910-
details: bulk_write_error.details,
909+
code: insert_error.code,
910+
code_name: insert_error.code_name,
911+
message: insert_error.message,
912+
details: insert_error.details,
911913
};
912914
Ok(WriteFailure::WriteError(write_error))
913915
} else if let Some(wc_error) = bulk.write_concern_error {
@@ -993,14 +995,15 @@ pub enum GridFsFileIdentifier {
993995
Id(Bson),
994996
}
995997

996-
/// Translates ErrorKind::BulkWriteError cases to ErrorKind::WriteErrors, leaving all other errors
997-
/// untouched.
998-
pub(crate) fn convert_bulk_errors(error: Error) -> Error {
998+
/// Translates ErrorKind::InsertMany to ErrorKind::Write, leaving all other errors untouched.
999+
pub(crate) fn convert_insert_many_error(error: Error) -> Error {
9991000
match *error.kind {
1000-
ErrorKind::BulkWrite(bulk_failure) => match WriteFailure::from_bulk_failure(bulk_failure) {
1001-
Ok(failure) => Error::new(ErrorKind::Write(failure), Some(error.labels)),
1002-
Err(e) => e,
1003-
},
1001+
ErrorKind::InsertMany(insert_many_error) => {
1002+
match WriteFailure::from_insert_many_error(insert_many_error) {
1003+
Ok(failure) => Error::new(ErrorKind::Write(failure), Some(error.labels)),
1004+
Err(e) => e,
1005+
}
1006+
}
10041007
_ => error,
10051008
}
10061009
}

0 commit comments

Comments
 (0)