use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("HTTP error: {0}")]
Http(String),
#[error("Status {0}: {1}")]
Status(u16, String),
#[error("Unauthorized: {0}")]
Unauthorized(String),
#[error("Forbidden: {0}")]
Forbidden(String),
#[error("DI error: {0}")]
Di(String),
#[error("Routing error: {0}")]
Routing(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Internal error: {0}")]
Internal(String),
#[error("{0}")]
Message(String),
#[error("{0}")]
Validation(String),
#[error("{0}")]
NotFound(String),
#[error("{0}")]
Conflict(String),
}
impl Error {
pub fn status_code(&self) -> u16 {
match self {
Error::Http(_) => 400,
Error::Status(code, _) => *code,
Error::Unauthorized(_) => 401,
Error::Forbidden(_) => 403,
Error::Di(_) => 500,
Error::Routing(_) => 404,
Error::Serialization(_) => 400,
Error::Internal(_) => 500,
Error::Message(_) => 500,
Error::Validation(_) => 400,
Error::NotFound(_) => 404,
Error::Conflict(_) => 409,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;