Move Error struct to own file

main
Elnu 2 years ago
parent 6de079b279
commit 98dc007fa4

@ -0,0 +1,50 @@
use actix_web::HttpResponse;
pub enum Error {
InvalidOrigin,
InvalidBody,
InvalidUrl,
InvalidFields,
InvalidContentId,
InvalidParent,
EmailRequired,
NameRequired,
DatabaseAccessError,
DatabaseInternalError,
SanitizationError,
PageFetchError,
}
impl Error {
pub fn to_http_response(&self) -> HttpResponse {
match self {
Self::InvalidOrigin
| Self::InvalidBody
| Self::InvalidUrl
| Self::InvalidFields
| Self::InvalidContentId
| Self::InvalidParent
| Self::EmailRequired
| Self::NameRequired => HttpResponse::BadRequest(),
Self::DatabaseAccessError
| Self::DatabaseInternalError
| Self::SanitizationError
| Self::PageFetchError => HttpResponse::InternalServerError(),
}
.reason(match self {
Self::InvalidOrigin => "invalid request origin",
Self::InvalidBody => "invalid request body",
Self::InvalidUrl => "invalid request url",
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",
})
.finish()
}
}

@ -1,10 +1,13 @@
mod comment; mod comment;
use actix_cors::Cors;
pub use comment::*; pub use comment::*;
mod database; mod database;
pub use database::Database; pub use database::Database;
mod error;
pub use error::Error;
use actix_cors::Cors;
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer}; use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer};
use clap::Parser; use clap::Parser;
use sanitize_html::{errors::SanitizeError, rules::predefined::DEFAULT, sanitize_str}; use sanitize_html::{errors::SanitizeError, rules::predefined::DEFAULT, sanitize_str};
@ -19,55 +22,6 @@ struct AppState {
arguments: Arguments, arguments: Arguments,
} }
enum Error {
InvalidOrigin,
InvalidBody,
InvalidUrl,
InvalidFields,
InvalidContentId,
InvalidParent,
EmailRequired,
NameRequired,
DatabaseAccessError,
DatabaseInternalError,
SanitizationError,
PageFetchError,
}
impl Error {
fn to_http_response(&self) -> HttpResponse {
match self {
Self::InvalidOrigin
| Self::InvalidBody
| Self::InvalidUrl
| Self::InvalidFields
| Self::InvalidContentId
| Self::InvalidParent
| Self::EmailRequired
| Self::NameRequired => HttpResponse::BadRequest(),
Self::DatabaseAccessError
| Self::DatabaseInternalError
| Self::SanitizationError
| Self::PageFetchError => HttpResponse::InternalServerError(),
}
.reason(match self {
Self::InvalidOrigin => "invalid request origin",
Self::InvalidBody => "invalid request body",
Self::InvalidUrl => "invalid request url",
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",
})
.finish()
}
}
impl AppState { impl AppState {
fn get_db<'a>(&'a self, origin: Option<String>) -> Result<MutexGuard<'a, Database>, Error> { fn get_db<'a>(&'a self, origin: Option<String>) -> Result<MutexGuard<'a, Database>, Error> {
let origin = match origin { let origin = match origin {

Loading…
Cancel
Save