use actix_web::{http::StatusCode, HttpResponse, ResponseError}; pub(crate) trait OptionExt { fn req(self) -> Result; } #[derive(Debug, thiserror::Error)] pub(crate) enum Error { #[error("{0}")] Accounts(#[from] hyaenidae_accounts::Error), #[error("{0}")] Profiles(#[from] hyaenidae_profiles::Error), #[error("{0}")] Render(std::io::Error), #[error("{0}")] Sled(#[from] sled::Error), #[error("{0}")] Transaction(#[from] sled::transaction::TransactionError), #[error("{0}")] Json(#[from] serde_json::Error), #[error("{0}")] Url(#[from] url::ParseError), #[error("{0}")] Webfinger(#[from] actix_webfinger::FetchError), #[error("Required data was not present")] Required, #[error("Panic in blocking operation")] Panic, } impl ResponseError for Error { fn status_code(&self) -> StatusCode { StatusCode::SEE_OTHER } fn error_response(&self) -> HttpResponse { if matches!(self, Error::Required) { return crate::to_404(); } crate::to_500() } } impl OptionExt for Option { fn req(self) -> Result { self.ok_or_else(|| Error::Required) } } impl From for Error { fn from(e: hyaenidae_profiles::store::StoreError) -> Self { Error::Profiles(From::from(e)) } } impl From for Error { fn from(e: hyaenidae_profiles::apub::StoreError) -> Self { Error::Profiles(From::from(e)) } } impl From for Error { fn from(_: actix_web::error::BlockingError) -> Self { Self::Panic } }