diff --git a/src/collection.rs b/src/collection.rs index f18879e..2542fc0 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -12,7 +12,7 @@ use bsonc; use bson::Document; -use super::Result; +use super::{Result,BulkOperationResult,BulkOperationError}; use super::CommandAndFindOptions; use super::{BsoncError,InvalidParamsError}; use super::bsonc::Bsonc; @@ -873,7 +873,7 @@ impl<'a>BulkOperation<'a> { /// multiple times. /// /// Returns a document with an overview of the bulk operation if successfull. - pub fn execute(self) -> Result { + pub fn execute(self) -> BulkOperationResult { // Bsonc to store the reply let mut reply = Bsonc::new(); // Empty error that might be filled @@ -889,13 +889,15 @@ impl<'a>BulkOperation<'a> { ) }; + let document = match reply.as_document() { + Ok(document) => document, + Err(error) => return Err(BulkOperationError{error: error.into(), reply: doc!{}}) + }; + if return_value != 0 { - match reply.as_document() { - Ok(document) => return Ok(document), - Err(error) => return Err(error.into()) - } + Ok(document) } else { - Err(error.into()) + Err(BulkOperationError{error: error.into(), reply: document}) } } } diff --git a/src/error.rs b/src/error.rs index ad41580..bb28f41 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,7 @@ use std::fmt; use std::borrow::Cow; use std::ffi::CStr; -use bson::{DecoderError,EncoderError,ValueAccessError}; +use bson::{DecoderError,EncoderError,ValueAccessError,Document}; use mongoc::bindings; @@ -256,7 +256,7 @@ impl fmt::Debug for BsoncError { impl fmt::Display for BsoncError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", &self.get_message()) + write!(f, "{}", self.get_message()) } } @@ -299,6 +299,27 @@ impl From for MongoError { } } +/// Error returned by a bulk operation that includes a report in the reply document. +#[derive(Debug)] +pub struct BulkOperationError { + /// Returned error + pub error: MongoError, + /// Error report + pub reply: Document +} + +impl fmt::Display for BulkOperationError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Bulk operation error {}", self.error) + } +} + +impl error::Error for BulkOperationError { + fn description(&self) -> &str { + "Error returned by a bulk operation that includes a report in the reply document" + } +} + #[cfg(test)] mod tests { use super::{BsoncError,MongoErrorDomain,MongoErrorCode}; diff --git a/src/lib.rs b/src/lib.rs index 9165058..4c1383a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,11 +49,14 @@ pub mod write_concern; mod bsonc; mod error; -pub use error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError}; +pub use error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError,BulkOperationError}; /// Result that's used in all functions that perform operations on the database. pub type Result = result::Result; +/// Result that's used in bulk operations. +pub type BulkOperationResult = result::Result; + static MONGOC_INIT: Once = ONCE_INIT; /// Init mongo driver, needs to be called once before doing diff --git a/tests/bulk_operation.rs b/tests/bulk_operation.rs index ce2eba4..242bbe8 100644 --- a/tests/bulk_operation.rs +++ b/tests/bulk_operation.rs @@ -16,7 +16,7 @@ fn test_execute_error() { assert!(result.is_err()); let error_message = format!("{:?}", result.err().unwrap()); - assert_eq!(error_message, "MongoError (BsoncError: Command/CommandInvalidArg - Cannot do an empty bulk write)"); + assert_eq!(error_message, "BulkOperationError { error: MongoError (BsoncError: Command/CommandInvalidArg - Cannot do an empty bulk write), reply: OrderedDocument({}) }"); } #[test]