fabaccess-bffh/bffhd/error.rs
2021-12-15 23:42:16 +01:00

72 lines
1.5 KiB
Rust

use std::io;
use std::fmt;
use rsasl::SaslError;
use crate::db::DBError;
#[derive(Debug)]
/// Shared error type
pub enum Error {
SASL(SaslError),
IO(io::Error),
Boxed(Box<dyn std::error::Error>),
Capnp(capnp::Error),
DB(DBError),
Denied,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::SASL(e) => {
write!(f, "SASL Error: {}", e)
},
Error::IO(e) => {
write!(f, "IO Error: {}", e)
},
Error::Boxed(e) => {
write!(f, "{}", e)
},
Error::Capnp(e) => {
write!(f, "Cap'n Proto Error: {}", e)
},
Error::DB(e) => {
write!(f, "DB Error: {:?}", e)
},
Error::Denied => {
write!(f, "You do not have the permission required to do that.")
}
}
}
}
impl From<SaslError> for Error {
fn from(e: SaslError) -> Error {
Error::SASL(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::IO(e)
}
}
impl From<Box<dyn std::error::Error>> for Error {
fn from(e: Box<dyn std::error::Error>) -> Error {
Error::Boxed(e)
}
}
impl From<capnp::Error> for Error {
fn from(e: capnp::Error) -> Error {
Error::Capnp(e)
}
}
impl From<DBError> for Error {
fn from(e: DBError) -> Error {
Error::DB(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;