Merge pull request #31 from thijsc/bulk_operation_error

Bulk operation result which includes reply
pull/33/head
Thijs Cadier 7 years ago committed by GitHub
commit 1939a5d6ee

@ -12,7 +12,7 @@ use bsonc;
use bson::Document; use bson::Document;
use super::Result; use super::{Result,BulkOperationResult,BulkOperationError};
use super::CommandAndFindOptions; use super::CommandAndFindOptions;
use super::{BsoncError,InvalidParamsError}; use super::{BsoncError,InvalidParamsError};
use super::bsonc::Bsonc; use super::bsonc::Bsonc;
@ -873,7 +873,7 @@ impl<'a>BulkOperation<'a> {
/// multiple times. /// multiple times.
/// ///
/// Returns a document with an overview of the bulk operation if successfull. /// Returns a document with an overview of the bulk operation if successfull.
pub fn execute(self) -> Result<Document> { pub fn execute(self) -> BulkOperationResult<Document> {
// Bsonc to store the reply // Bsonc to store the reply
let mut reply = Bsonc::new(); let mut reply = Bsonc::new();
// Empty error that might be filled // 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 { if return_value != 0 {
match reply.as_document() { Ok(document)
Ok(document) => return Ok(document),
Err(error) => return Err(error.into())
}
} else { } else {
Err(error.into()) Err(BulkOperationError{error: error.into(), reply: document})
} }
} }
} }

@ -3,7 +3,7 @@ use std::fmt;
use std::borrow::Cow; use std::borrow::Cow;
use std::ffi::CStr; use std::ffi::CStr;
use bson::{DecoderError,EncoderError,ValueAccessError}; use bson::{DecoderError,EncoderError,ValueAccessError,Document};
use mongoc::bindings; use mongoc::bindings;
@ -256,7 +256,7 @@ impl fmt::Debug for BsoncError {
impl fmt::Display for BsoncError { impl fmt::Display for BsoncError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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<InvalidParamsError> 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)] #[cfg(test)]
mod tests { mod tests {
use super::{BsoncError,MongoErrorDomain,MongoErrorCode}; use super::{BsoncError,MongoErrorDomain,MongoErrorCode};

@ -49,11 +49,14 @@ pub mod write_concern;
mod bsonc; mod bsonc;
mod error; 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. /// Result that's used in all functions that perform operations on the database.
pub type Result<T> = result::Result<T, MongoError>; pub type Result<T> = result::Result<T, MongoError>;
/// Result that's used in bulk operations.
pub type BulkOperationResult<T> = result::Result<T, BulkOperationError>;
static MONGOC_INIT: Once = ONCE_INIT; static MONGOC_INIT: Once = ONCE_INIT;
/// Init mongo driver, needs to be called once before doing /// Init mongo driver, needs to be called once before doing

@ -16,7 +16,7 @@ fn test_execute_error() {
assert!(result.is_err()); assert!(result.is_err());
let error_message = format!("{:?}", result.err().unwrap()); 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] #[test]

Loading…
Cancel
Save