2021-01-09 04:35:35 +00:00
|
|
|
use crate::rendered;
|
2020-12-16 02:40:41 +00:00
|
|
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
|
|
|
|
2021-01-06 08:21:37 +00:00
|
|
|
pub(crate) trait OptionExt<T> {
|
|
|
|
fn req(self) -> Result<T, Error>;
|
|
|
|
}
|
|
|
|
|
2020-12-16 02:40:41 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum Error {
|
|
|
|
#[error("{0}")]
|
|
|
|
Accounts(#[from] hyaenidae_accounts::Error),
|
|
|
|
|
2021-01-06 08:21:37 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
Profiles(#[from] hyaenidae_profiles::Error),
|
|
|
|
|
2020-12-16 02:40:41 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
Render(std::io::Error),
|
|
|
|
|
2021-01-06 08:21:37 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
Sled(#[from] sled::Error),
|
|
|
|
|
2021-01-14 04:46:34 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
Transaction(#[from] sled::transaction::TransactionError),
|
|
|
|
|
|
|
|
#[error("{0}")]
|
|
|
|
Json(#[from] serde_json::Error),
|
|
|
|
|
2021-01-06 08:21:37 +00:00
|
|
|
#[error("Required data was not present")]
|
|
|
|
Required,
|
2021-01-14 04:46:34 +00:00
|
|
|
|
|
|
|
#[error("Panic in blocking operation")]
|
|
|
|
Panic,
|
2020-12-16 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 04:35:35 +00:00
|
|
|
impl ResponseError for Error {
|
2020-12-16 02:40:41 +00:00
|
|
|
fn status_code(&self) -> StatusCode {
|
2021-01-09 04:35:35 +00:00
|
|
|
match self {
|
2021-01-14 04:46:34 +00:00
|
|
|
Error::Render(_)
|
|
|
|
| Error::Accounts(_)
|
|
|
|
| Error::Profiles(_)
|
|
|
|
| Error::Sled(_)
|
|
|
|
| Error::Transaction(_)
|
|
|
|
| Error::Json(_)
|
|
|
|
| Error::Panic => StatusCode::INTERNAL_SERVER_ERROR,
|
2021-01-10 01:49:33 +00:00
|
|
|
Error::Required => StatusCode::SEE_OTHER,
|
2020-12-16 02:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
2021-01-10 01:49:33 +00:00
|
|
|
if matches!(self, Error::Required) {
|
|
|
|
return crate::to_404();
|
|
|
|
}
|
|
|
|
|
2020-12-16 02:40:41 +00:00
|
|
|
match rendered(HttpResponse::build(self.status_code()), |cursor| {
|
2021-01-11 04:09:47 +00:00
|
|
|
crate::templates::error(cursor, self.to_string(), true)
|
2020-12-16 02:40:41 +00:00
|
|
|
}) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(_) => HttpResponse::build(self.status_code())
|
|
|
|
.content_type(mime::TEXT_PLAIN.essence_str())
|
2021-01-09 04:35:35 +00:00
|
|
|
.body(self.to_string()),
|
2020-12-16 02:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 08:21:37 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 04:46:34 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|