hyaenidae/src/error.rs
asonix e0858b7b3d Move server/ to / and start i18ning
Currently i18n'd
- login page
- register page
- cookie page
- delete account confirmation page
- 404 page
- 500 page
- part of account settings
2021-01-28 20:25:31 -06:00

80 lines
1.8 KiB
Rust

use actix_web::{http::StatusCode, HttpResponse, ResponseError};
pub(crate) trait OptionExt<T> {
fn req(self) -> Result<T, Error>;
}
#[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<T> OptionExt<T> for Option<T> {
fn req(self) -> Result<T, Error> {
self.ok_or_else(|| Error::Required)
}
}
impl From<hyaenidae_profiles::store::StoreError> for Error {
fn from(e: hyaenidae_profiles::store::StoreError) -> Self {
Error::Profiles(From::from(e))
}
}
impl From<hyaenidae_profiles::apub::StoreError> for Error {
fn from(e: hyaenidae_profiles::apub::StoreError) -> Self {
Error::Profiles(From::from(e))
}
}
impl From<actix_web::error::BlockingError<Error>> for Error {
fn from(e: actix_web::error::BlockingError<Error>) -> Self {
match e {
actix_web::error::BlockingError::Error(e) => e,
_ => Error::Panic,
}
}
}