Add docs for error types

pull/15/head
Thijs Cadier 9 years ago
parent 4a13d6bae6
commit 816b6a1283

@ -7,11 +7,17 @@ use bson::{DecoderError,EncoderError,ValueAccessError};
use mongoc::bindings; use mongoc::bindings;
/// Wrapper for all errors that can occur in the driver.
pub enum MongoError { pub enum MongoError {
/// Error in the underlying C driver.
Bsonc(BsoncError), Bsonc(BsoncError),
/// Error decoding Bson.
Decoder(DecoderError), Decoder(DecoderError),
/// Error encoding Bson.
Encoder(EncoderError), Encoder(EncoderError),
/// Error accessing a value on a Bson document.
ValueAccessError(ValueAccessError), ValueAccessError(ValueAccessError),
/// Invalid params error that can be reported by the underlying C driver.
InvalidParams(InvalidParamsError) InvalidParams(InvalidParamsError)
} }
@ -79,10 +85,12 @@ impl From<ValueAccessError> for MongoError {
} }
} }
/// Error in the underlying C driver.
pub struct BsoncError { pub struct BsoncError {
inner: bindings::bson_error_t, inner: bindings::bson_error_t,
} }
/// MongoDB error domain.
#[derive(Debug,PartialEq)] #[derive(Debug,PartialEq)]
pub enum MongoErrorDomain { pub enum MongoErrorDomain {
Blank, Blank,
@ -103,6 +111,7 @@ pub enum MongoErrorDomain {
Unknown Unknown
} }
/// MongoDB error code.
#[derive(Debug,PartialEq)] #[derive(Debug,PartialEq)]
pub enum MongoErrorCode { pub enum MongoErrorCode {
Blank, Blank,
@ -151,10 +160,12 @@ impl BsoncError {
} }
} }
/// Wether the error has content.
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.inner.domain == 0 && self.inner.code == 0 self.inner.domain == 0 && self.inner.code == 0
} }
/// The error's domain.
pub fn domain(&self) -> MongoErrorDomain { pub fn domain(&self) -> MongoErrorDomain {
match self.inner.domain { match self.inner.domain {
0 => MongoErrorDomain::Blank, 0 => MongoErrorDomain::Blank,
@ -176,6 +187,7 @@ impl BsoncError {
} }
} }
/// The error's code.
pub fn code(&self) -> MongoErrorCode { pub fn code(&self) -> MongoErrorCode {
match self.inner.code { match self.inner.code {
0 => MongoErrorCode::Blank, 0 => MongoErrorCode::Blank,
@ -214,11 +226,13 @@ impl BsoncError {
} }
} }
/// The error's message.
pub fn get_message(&self) -> Cow<str> { pub fn get_message(&self) -> Cow<str> {
let cstr = unsafe { CStr::from_ptr(&self.inner.message as *const i8) }; let cstr = unsafe { CStr::from_ptr(&self.inner.message as *const i8) };
String::from_utf8_lossy(cstr.to_bytes()) String::from_utf8_lossy(cstr.to_bytes())
} }
#[doc(hidden)]
pub fn mut_inner(&mut self) -> &mut bindings::bson_error_t { pub fn mut_inner(&mut self) -> &mut bindings::bson_error_t {
&mut self.inner &mut self.inner
} }
@ -254,6 +268,7 @@ impl From<BsoncError> for MongoError {
} }
} }
/// Invalid params error that can be reported by the underlying C driver.
pub struct InvalidParamsError; pub struct InvalidParamsError;
impl fmt::Debug for InvalidParamsError { impl fmt::Debug for InvalidParamsError {

@ -49,8 +49,9 @@ pub mod write_concern;
mod bsonc; mod bsonc;
mod error; 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<T> = result::Result<T, MongoError>; pub type Result<T> = result::Result<T, MongoError>;
static MONGOC_INIT: Once = ONCE_INIT; static MONGOC_INIT: Once = ONCE_INIT;

Loading…
Cancel
Save