use std::fmt;
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[derive(Debug)]
pub struct StringError(pub String);
impl fmt::Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for StringError {}
#[derive(Debug)]
pub struct TowerError(pub BoxError);
impl fmt::Display for TowerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "tower middleware error: {}", self.0)
}
}
impl std::error::Error for TowerError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.0.as_ref())
}
}
impl actix_web::ResponseError for TowerError {
fn status_code(&self) -> actix_web::http::StatusCode {
actix_web::http::StatusCode::BAD_GATEWAY
}
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
actix_web::HttpResponse::build(self.status_code()).body(self.to_string())
}
}
impl From<BoxError> for TowerError {
fn from(e: BoxError) -> Self {
TowerError(e)
}
}