use std::fmt;
use std::io;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(io::Error),
BadRequest(String),
BodyTooLarge { limit: usize },
Unsupported(&'static str),
Config(String),
Json(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "io error: {e}"),
Error::BadRequest(m) => write!(f, "bad request: {m}"),
Error::BodyTooLarge { limit } => {
write!(f, "request body exceeds the {limit} byte limit")
}
Error::Unsupported(what) => write!(f, "unsupported: {what}"),
Error::Config(m) => write!(f, "config error: {m}"),
Error::Json(m) => write!(f, "json error: {m}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}