use bcrypt::BcryptError; use diesel::result::Error as DieselErr; use std::convert::From; use std::error::Error as StdError; use std::fmt; #[derive(Debug)] pub enum Error { DieselError(DieselErr), PasswordError(BcryptError), CommunicationError(reqwest::Error), ParseError(serde_json::error::Error), AlreadyExists, NotFound, InternalError, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::DieselError(ref e) => e.fmt(f), Error::PasswordError(ref e) => e.fmt(f), Error::CommunicationError(ref e) => e.fmt(f), Error::ParseError(ref e) => e.fmt(f), Error::AlreadyExists => f.write_str("AlreadyExists"), Error::NotFound => f.write_str("NotFound"), Error::InternalError => f.write_str("InternalError"), } } } impl From for Error { fn from(e: serde_json::error::Error) -> Error { Error::ParseError(e) } } impl From for Error { fn from(e: reqwest::Error) -> Error { Error::CommunicationError(e) } } impl From for Error { fn from(e: DieselErr) -> Error { Error::DieselError(e) } } impl From for Error { fn from(e: BcryptError) -> Error { Error::PasswordError(e) } } impl StdError for Error {}