parent
3f2f72a278
commit
ea3e9bcd5a
@ -1,50 +1,72 @@
|
|||||||
use actix_web::HttpResponse;
|
use crate::Database;
|
||||||
|
|
||||||
|
use actix_web::{HttpResponse, error::BlockingError};
|
||||||
|
use std::{sync::{PoisonError, MutexGuard}, string::FromUtf8Error};
|
||||||
|
use sanitize_html::errors::SanitizeError;
|
||||||
|
use validator::ValidationErrors;
|
||||||
|
use derive_more::From;
|
||||||
|
|
||||||
|
#[derive(From, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
InvalidOrigin,
|
InvalidOrigin,
|
||||||
InvalidBody,
|
InvalidBodyEncoding(FromUtf8Error),
|
||||||
|
InvalidBodyJson(serde_json::Error),
|
||||||
InvalidUrl,
|
InvalidUrl,
|
||||||
InvalidFields,
|
InvalidFields(ValidationErrors),
|
||||||
InvalidContentId,
|
InvalidContentId,
|
||||||
InvalidParent,
|
InvalidParent,
|
||||||
EmailRequired,
|
EmailRequired,
|
||||||
NameRequired,
|
NameRequired,
|
||||||
DatabaseAccessError,
|
DatabaseAccessBlockingError(BlockingError),
|
||||||
DatabaseInternalError,
|
DatabaseAccessPoisonError,
|
||||||
SanitizationError,
|
DatabaseInternalError(rusqlite::Error),
|
||||||
PageFetchError,
|
SanitizationError(SanitizeError),
|
||||||
|
PageFetchError(reqwest::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
pub fn to_http_response(&self) -> HttpResponse {
|
pub fn to_http_response(&self) -> HttpResponse {
|
||||||
match self {
|
match self {
|
||||||
Self::InvalidOrigin
|
Self::InvalidOrigin
|
||||||
| Self::InvalidBody
|
| Self::InvalidBodyEncoding(_)
|
||||||
|
| Self::InvalidBodyJson(_)
|
||||||
| Self::InvalidUrl
|
| Self::InvalidUrl
|
||||||
| Self::InvalidFields
|
| Self::InvalidFields(_)
|
||||||
| Self::InvalidContentId
|
| Self::InvalidContentId
|
||||||
| Self::InvalidParent
|
| Self::InvalidParent
|
||||||
| Self::EmailRequired
|
| Self::EmailRequired
|
||||||
| Self::NameRequired => HttpResponse::BadRequest(),
|
| Self::NameRequired => HttpResponse::BadRequest(),
|
||||||
Self::DatabaseAccessError
|
Self::DatabaseAccessBlockingError(_)
|
||||||
| Self::DatabaseInternalError
|
| Self::DatabaseAccessPoisonError
|
||||||
| Self::SanitizationError
|
| Self::DatabaseInternalError(_)
|
||||||
| Self::PageFetchError => HttpResponse::InternalServerError(),
|
| Self::SanitizationError(_)
|
||||||
|
| Self::PageFetchError(_) => {
|
||||||
|
eprintln!("{:?}", self);
|
||||||
|
HttpResponse::InternalServerError()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
.reason(match self {
|
.reason(match self {
|
||||||
Self::InvalidOrigin => "invalid request origin",
|
Self::InvalidOrigin => "invalid request origin",
|
||||||
Self::InvalidBody => "invalid request body",
|
Self::InvalidBodyEncoding(_)
|
||||||
|
| Self::InvalidBodyJson(_) => "invalid request body",
|
||||||
Self::InvalidUrl => "invalid request url",
|
Self::InvalidUrl => "invalid request url",
|
||||||
Self::InvalidFields => "invalid request field",
|
Self::InvalidFields(_) => "invalid request field",
|
||||||
Self::InvalidContentId => "invalid request content id",
|
Self::InvalidContentId => "invalid request content id",
|
||||||
Self::InvalidParent => "invalid comment parent",
|
Self::InvalidParent => "invalid comment parent",
|
||||||
Self::EmailRequired => "comment email required",
|
Self::EmailRequired => "comment email required",
|
||||||
Self::NameRequired => "comment name required",
|
Self::NameRequired => "comment name required",
|
||||||
Self::DatabaseAccessError => "database access error",
|
Self::DatabaseAccessBlockingError(_)
|
||||||
Self::DatabaseInternalError => "database internal error",
|
| Self::DatabaseAccessPoisonError => "database access error",
|
||||||
Self::SanitizationError => "comment sanitization error",
|
Self::DatabaseInternalError(_) => "database internal error",
|
||||||
Self::PageFetchError => "page fetch error",
|
Self::SanitizationError(_) => "comment sanitization error",
|
||||||
|
Self::PageFetchError(_) => "page fetch error",
|
||||||
})
|
})
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> From<PoisonError<MutexGuard<'a, Database>>> for Error {
|
||||||
|
fn from(_: PoisonError<MutexGuard<'a, Database>>) -> Self {
|
||||||
|
Self::DatabaseAccessPoisonError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue