use std::fmt::Display;
use hyper::StatusCode;
#[derive(Debug)]
pub struct Error {
pub code: StatusCode,
pub message: String,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "error: {}", self.message)
}
}
impl std::error::Error for Error {
}
impl Error {
pub fn response_builder(&self) -> hyper::http::response::Builder {
hyper::Response::builder().status(self.code)
}
pub fn bad_request(message: String) -> Self {
Error{code: StatusCode::BAD_REQUEST, message}
}
pub fn not_found(message: String) -> Self {
Error{code: StatusCode::NOT_FOUND, message}
}
pub fn unauthorized(message: String) -> Self {
Error{code: StatusCode::UNAUTHORIZED, message}
}
pub fn internal(message: String) -> Self {
Error{code: StatusCode::INTERNAL_SERVER_ERROR, message}
}
pub fn method_not_allowed(message: String) -> Self {
Error{code: StatusCode::METHOD_NOT_ALLOWED, message}
}
}