1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
use mongodb_cursor_pagination::error::CursorError;
use std::{error, fmt, io};

/// Possible errors that can arise during parsing and creating a cursor.
#[derive(Debug)]
pub enum ServiceError {
    IoError(io::Error),
    ParseError(String),
    MongoError(mongodb::error::Error),
    ConnectionError(String),
    InvalidCursor(String),
    NotFound(String),
    Unknown(String),
}

impl From<io::Error> for ServiceError {
    fn from(err: io::Error) -> ServiceError {
        ServiceError::IoError(err)
    }
}

impl From<bson::EncoderError> for ServiceError {
    fn from(err: bson::EncoderError) -> ServiceError {
        ServiceError::ParseError(err.to_string())
    }
}

impl From<bson::DecoderError> for ServiceError {
    fn from(err: bson::DecoderError) -> ServiceError {
        ServiceError::ParseError(err.to_string())
    }
}

impl From<mongodb::error::Error> for ServiceError {
    fn from(err: mongodb::error::Error) -> ServiceError {
        ServiceError::MongoError(err)
    }
}

impl From<&str> for ServiceError {
    fn from(message: &str) -> ServiceError {
        ServiceError::Unknown(message.to_owned())
    }
}

impl From<String> for ServiceError {
    fn from(message: String) -> ServiceError {
        ServiceError::Unknown(message)
    }
}

impl From<CursorError> for ServiceError {
    fn from(err: CursorError) -> ServiceError {
        match err {
            CursorError::IoError(err) => ServiceError::IoError(err),
            CursorError::InvalidCursor(inner)
            | CursorError::Unknown(inner)
            | CursorError::InvalidId(inner) => ServiceError::InvalidCursor(inner),
        }
    }
}

impl fmt::Display for ServiceError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ServiceError::IoError(ref inner) => inner.fmt(fmt),
            ServiceError::MongoError(ref inner) => inner.fmt(fmt),
            ServiceError::InvalidCursor(ref cursor) => {
                write!(fmt, "Invalid cursor - unable to parse: {:?}", cursor)
            }
            ServiceError::ConnectionError(ref inner)
            | ServiceError::ParseError(ref inner)
            | ServiceError::NotFound(ref inner)
            | ServiceError::Unknown(ref inner) => inner.fmt(fmt),
        }
    }
}

#[allow(deprecated)]
impl error::Error for ServiceError {
    fn description(&self) -> &str {
        match *self {
            ServiceError::IoError(ref inner) => inner.description(),
            ServiceError::MongoError(ref inner) => inner.description(),
            ServiceError::InvalidCursor(_) => "Invalid cursor value",
            ServiceError::Unknown(ref inner)
            | ServiceError::ParseError(ref inner)
            | ServiceError::ConnectionError(ref inner)
            | ServiceError::NotFound(ref inner) => inner,
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            ServiceError::IoError(ref inner) => Some(inner),
            ServiceError::MongoError(ref inner) => Some(inner),
            _ => None,
        }
    }
}