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 {
|
||||
InvalidOrigin,
|
||||
InvalidBody,
|
||||
InvalidBodyEncoding(FromUtf8Error),
|
||||
InvalidBodyJson(serde_json::Error),
|
||||
InvalidUrl,
|
||||
InvalidFields,
|
||||
InvalidFields(ValidationErrors),
|
||||
InvalidContentId,
|
||||
InvalidParent,
|
||||
EmailRequired,
|
||||
NameRequired,
|
||||
DatabaseAccessError,
|
||||
DatabaseInternalError,
|
||||
SanitizationError,
|
||||
PageFetchError,
|
||||
DatabaseAccessBlockingError(BlockingError),
|
||||
DatabaseAccessPoisonError,
|
||||
DatabaseInternalError(rusqlite::Error),
|
||||
SanitizationError(SanitizeError),
|
||||
PageFetchError(reqwest::Error),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn to_http_response(&self) -> HttpResponse {
|
||||
match self {
|
||||
Self::InvalidOrigin
|
||||
| Self::InvalidBody
|
||||
| Self::InvalidBodyEncoding(_)
|
||||
| Self::InvalidBodyJson(_)
|
||||
| Self::InvalidUrl
|
||||
| Self::InvalidFields
|
||||
| Self::InvalidFields(_)
|
||||
| Self::InvalidContentId
|
||||
| Self::InvalidParent
|
||||
| Self::EmailRequired
|
||||
| Self::NameRequired => HttpResponse::BadRequest(),
|
||||
Self::DatabaseAccessError
|
||||
| Self::DatabaseInternalError
|
||||
| Self::SanitizationError
|
||||
| Self::PageFetchError => HttpResponse::InternalServerError(),
|
||||
Self::DatabaseAccessBlockingError(_)
|
||||
| Self::DatabaseAccessPoisonError
|
||||
| Self::DatabaseInternalError(_)
|
||||
| Self::SanitizationError(_)
|
||||
| Self::PageFetchError(_) => {
|
||||
eprintln!("{:?}", self);
|
||||
HttpResponse::InternalServerError()
|
||||
},
|
||||
}
|
||||
.reason(match self {
|
||||
Self::InvalidOrigin => "invalid request origin",
|
||||
Self::InvalidBody => "invalid request body",
|
||||
Self::InvalidBodyEncoding(_)
|
||||
| Self::InvalidBodyJson(_) => "invalid request body",
|
||||
Self::InvalidUrl => "invalid request url",
|
||||
Self::InvalidFields => "invalid request field",
|
||||
Self::InvalidFields(_) => "invalid request field",
|
||||
Self::InvalidContentId => "invalid request content id",
|
||||
Self::InvalidParent => "invalid comment parent",
|
||||
Self::EmailRequired => "comment email required",
|
||||
Self::NameRequired => "comment name required",
|
||||
Self::DatabaseAccessError => "database access error",
|
||||
Self::DatabaseInternalError => "database internal error",
|
||||
Self::SanitizationError => "comment sanitization error",
|
||||
Self::PageFetchError => "page fetch error",
|
||||
Self::DatabaseAccessBlockingError(_)
|
||||
| Self::DatabaseAccessPoisonError => "database access error",
|
||||
Self::DatabaseInternalError(_) => "database internal error",
|
||||
Self::SanitizationError(_) => "comment sanitization error",
|
||||
Self::PageFetchError(_) => "page fetch error",
|
||||
})
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<PoisonError<MutexGuard<'a, Database>>> for Error {
|
||||
fn from(_: PoisonError<MutexGuard<'a, Database>>) -> Self {
|
||||
Self::DatabaseAccessPoisonError
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue