From 816b6a1283ef15054c25406fa48b3bf2caccc6e8 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Sat, 23 Jan 2016 18:28:18 +0100 Subject: [PATCH] Add docs for error types --- src/error.rs | 15 +++++++++++++++ src/lib.rs | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index cec18b5..7128798 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,11 +7,17 @@ use bson::{DecoderError,EncoderError,ValueAccessError}; use mongoc::bindings; +/// Wrapper for all errors that can occur in the driver. pub enum MongoError { + /// Error in the underlying C driver. Bsonc(BsoncError), + /// Error decoding Bson. Decoder(DecoderError), + /// Error encoding Bson. Encoder(EncoderError), + /// Error accessing a value on a Bson document. ValueAccessError(ValueAccessError), + /// Invalid params error that can be reported by the underlying C driver. InvalidParams(InvalidParamsError) } @@ -79,10 +85,12 @@ impl From for MongoError { } } +/// Error in the underlying C driver. pub struct BsoncError { inner: bindings::bson_error_t, } +/// MongoDB error domain. #[derive(Debug,PartialEq)] pub enum MongoErrorDomain { Blank, @@ -103,6 +111,7 @@ pub enum MongoErrorDomain { Unknown } +/// MongoDB error code. #[derive(Debug,PartialEq)] pub enum MongoErrorCode { Blank, @@ -151,10 +160,12 @@ impl BsoncError { } } + /// Wether the error has content. pub fn is_empty(&self) -> bool { self.inner.domain == 0 && self.inner.code == 0 } + /// The error's domain. pub fn domain(&self) -> MongoErrorDomain { match self.inner.domain { 0 => MongoErrorDomain::Blank, @@ -176,6 +187,7 @@ impl BsoncError { } } + /// The error's code. pub fn code(&self) -> MongoErrorCode { match self.inner.code { 0 => MongoErrorCode::Blank, @@ -214,11 +226,13 @@ impl BsoncError { } } + /// The error's message. pub fn get_message(&self) -> Cow { let cstr = unsafe { CStr::from_ptr(&self.inner.message as *const i8) }; String::from_utf8_lossy(cstr.to_bytes()) } + #[doc(hidden)] pub fn mut_inner(&mut self) -> &mut bindings::bson_error_t { &mut self.inner } @@ -254,6 +268,7 @@ impl From for MongoError { } } +/// Invalid params error that can be reported by the underlying C driver. pub struct InvalidParamsError; impl fmt::Debug for InvalidParamsError { diff --git a/src/lib.rs b/src/lib.rs index 2166724..d0227ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,8 +49,9 @@ pub mod write_concern; mod bsonc; mod error; -pub use error::{MongoError,BsoncError,InvalidParamsError}; +pub use error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError}; +/// Result that's used in all functions that perform operations on the database. pub type Result = result::Result; static MONGOC_INIT: Once = ONCE_INIT;