flodgatt/src/err.rs

82 lines
1.7 KiB
Rust
Raw Normal View History

2020-04-20 00:02:24 +02:00
use crate::config;
2020-04-18 03:56:31 +02:00
use crate::request;
use crate::response;
2020-04-20 00:02:24 +02:00
use std::fmt;
2020-04-18 03:56:31 +02:00
pub enum Error {
2020-04-20 00:02:24 +02:00
Response(response::Error),
Logger(log::SetLoggerError),
2020-04-18 03:56:31 +02:00
Postgres(request::Error),
Unrecoverable,
StdIo(std::io::Error),
2020-04-20 00:02:24 +02:00
Config(config::Error),
}
2020-04-18 03:56:31 +02:00
impl Error {
pub fn log(msg: impl fmt::Display) {
eprintln!("{}", msg);
}
}
2020-04-18 03:56:31 +02:00
impl std::error::Error for Error {}
2020-04-20 00:02:24 +02:00
2020-04-18 03:56:31 +02:00
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self)
}
}
2020-04-18 03:56:31 +02:00
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
2020-04-18 03:56:31 +02:00
use Error::*;
write!(
f,
"{}",
match self {
2020-04-20 00:02:24 +02:00
Response(e) => format!("{}", e),
Logger(e) => format!("{}", e),
StdIo(e) => format!("{}", e),
Postgres(e) => format!("could not connect to Postgres.\n{:7}{}", "", e),
2020-04-20 00:02:24 +02:00
Config(e) => format!("{}", e),
Unrecoverable => "Flodgatt will now shut down.".into(),
}
)
}
}
2020-04-20 00:02:24 +02:00
#[doc(hidden)]
2020-04-18 03:56:31 +02:00
impl From<request::Error> for Error {
fn from(e: request::Error) -> Self {
Self::Postgres(e)
}
}
2020-04-20 00:02:24 +02:00
#[doc(hidden)]
2020-04-18 03:56:31 +02:00
impl From<response::Error> for Error {
fn from(e: response::Error) -> Self {
2020-04-20 00:02:24 +02:00
Self::Response(e)
}
}
2020-04-20 00:02:24 +02:00
#[doc(hidden)]
impl From<config::Error> for Error {
fn from(e: config::Error) -> Self {
Self::Config(e)
}
}
2020-04-20 00:02:24 +02:00
#[doc(hidden)]
2020-04-18 03:56:31 +02:00
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::StdIo(e)
}
}
2020-04-20 00:02:24 +02:00
#[doc(hidden)]
2020-04-18 03:56:31 +02:00
impl From<log::SetLoggerError> for Error {
fn from(e: log::SetLoggerError) -> Self {
Self::Logger(e)
}
}