2024-06-28 20:28:43 +00:00
|
|
|
use actix_web::body::BoxBody;
|
|
|
|
use actix_web::http::StatusCode;
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::{Display, Formatter};
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
|
|
|
|
/// Custom error to ease controller writing
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum HttpErr {
|
|
|
|
Err(anyhow::Error),
|
|
|
|
HTTPResponse(HttpResponse),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for HttpErr {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
HttpErr::Err(err) => Display::fmt(err, f),
|
|
|
|
HttpErr::HTTPResponse(res) => {
|
|
|
|
Display::fmt(&format!("HTTP RESPONSE {}", res.status().as_str()), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl actix_web::error::ResponseError for HttpErr {
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
match self {
|
|
|
|
HttpErr::Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
HttpErr::HTTPResponse(r) => r.status(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn error_response(&self) -> HttpResponse<BoxBody> {
|
|
|
|
log::error!("Error while processing request! {}", self);
|
|
|
|
|
|
|
|
HttpResponse::InternalServerError().body("Failed to execute request!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<anyhow::Error> for HttpErr {
|
|
|
|
fn from(err: anyhow::Error) -> HttpErr {
|
|
|
|
HttpErr::Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for HttpErr {
|
|
|
|
fn from(value: serde_json::Error) -> Self {
|
|
|
|
HttpErr::Err(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Box<dyn Error>> for HttpErr {
|
|
|
|
fn from(value: Box<dyn Error>) -> Self {
|
|
|
|
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for HttpErr {
|
|
|
|
fn from(value: std::io::Error) -> Self {
|
|
|
|
HttpErr::Err(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::num::ParseIntError> for HttpErr {
|
|
|
|
fn from(value: std::num::ParseIntError) -> Self {
|
|
|
|
HttpErr::Err(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for HttpErr {
|
|
|
|
fn from(value: reqwest::Error) -> Self {
|
|
|
|
HttpErr::Err(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<reqwest::header::ToStrError> for HttpErr {
|
|
|
|
fn from(value: reqwest::header::ToStrError) -> Self {
|
|
|
|
HttpErr::Err(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<actix_web::Error> for HttpErr {
|
|
|
|
fn from(value: actix_web::Error) -> Self {
|
|
|
|
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-29 09:45:39 +00:00
|
|
|
impl From<actix::MailboxError> for HttpErr {
|
|
|
|
fn from(value: actix::MailboxError) -> Self {
|
|
|
|
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-29 12:43:56 +00:00
|
|
|
impl From<actix_identity::error::GetIdentityError> for HttpErr {
|
|
|
|
fn from(value: actix_identity::error::GetIdentityError) -> Self {
|
|
|
|
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<actix_identity::error::LoginError> for HttpErr {
|
|
|
|
fn from(value: actix_identity::error::LoginError) -> Self {
|
|
|
|
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-01 19:10:45 +00:00
|
|
|
impl From<openssl::error::ErrorStack> for HttpErr {
|
|
|
|
fn from(value: openssl::error::ErrorStack) -> Self {
|
|
|
|
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 20:28:43 +00:00
|
|
|
impl From<HttpResponse> for HttpErr {
|
|
|
|
fn from(value: HttpResponse) -> Self {
|
|
|
|
HttpErr::HTTPResponse(value)
|
|
|
|
}
|
|
|
|
}
|
2024-06-29 12:43:56 +00:00
|
|
|
|
2024-06-28 20:28:43 +00:00
|
|
|
pub type HttpResult = Result<HttpResponse, HttpErr>;
|