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 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<Document> {
pub fn execute(self) -> BulkOperationResult<Document> {
// 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})
}
}
}

@ -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<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)]
mod tests {
use super::{BsoncError,MongoErrorDomain,MongoErrorCode};

@ -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<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;
/// Init mongo driver, needs to be called once before doing

@ -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]

Loading…
Cancel
Save