From 98dc007fa490ee6d11303f54c08206fba4ea24d7 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 31 Jul 2022 11:05:55 -0700 Subject: [PATCH] Move Error struct to own file --- src/error.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 54 ++++------------------------------------------------ 2 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..415f600 --- /dev/null +++ b/src/error.rs @@ -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() + } +} diff --git a/src/main.rs b/src/main.rs index f1d3e8c..b4c70fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,13 @@ mod comment; -use actix_cors::Cors; pub use comment::*; mod 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 clap::Parser; use sanitize_html::{errors::SanitizeError, rules::predefined::DEFAULT, sanitize_str}; @@ -19,55 +22,6 @@ struct AppState { 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 { fn get_db<'a>(&'a self, origin: Option) -> Result, Error> { let origin = match origin {