main
Elnu 2 years ago
parent f68f2155c3
commit 6de079b279

@ -37,19 +37,20 @@ enum Error {
impl Error { impl Error {
fn to_http_response(&self) -> HttpResponse { fn to_http_response(&self) -> HttpResponse {
match self { match self {
Self::InvalidOrigin | Self::InvalidOrigin
Self::InvalidBody | | Self::InvalidBody
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::DatabaseAccessError
Self::DatabaseInternalError | | Self::DatabaseInternalError
Self::SanitizationError | | Self::SanitizationError
Self::PageFetchError => HttpResponse::InternalServerError(), | Self::PageFetchError => HttpResponse::InternalServerError(),
}.reason(match self { }
.reason(match self {
Self::InvalidOrigin => "invalid request origin", Self::InvalidOrigin => "invalid request origin",
Self::InvalidBody => "invalid request body", Self::InvalidBody => "invalid request body",
Self::InvalidUrl => "invalid request url", Self::InvalidUrl => "invalid request url",
@ -62,15 +63,13 @@ impl Error {
Self::DatabaseInternalError => "database internal error", Self::DatabaseInternalError => "database internal error",
Self::SanitizationError => "comment sanitization error", Self::SanitizationError => "comment sanitization error",
Self::PageFetchError => "page fetch error", Self::PageFetchError => "page fetch error",
}).finish() })
.finish()
} }
} }
impl AppState { impl AppState {
fn get_db<'a>( fn get_db<'a>(&'a self, origin: Option<String>) -> Result<MutexGuard<'a, Database>, Error> {
&'a self,
origin: Option<String>,
) -> Result<MutexGuard<'a, Database>, Error> {
let origin = match origin { let origin = match origin {
Some(origin) => origin, Some(origin) => origin,
None => return Err(Error::InvalidOrigin), None => return Err(Error::InvalidOrigin),
@ -141,7 +140,9 @@ async fn _get_comments(
Err(_) => return Err(Error::DatabaseInternalError), Err(_) => return Err(Error::DatabaseInternalError),
}, },
) )
}).await { })
.await
{
Ok(result) => result, Ok(result) => result,
Err(_) => Err(Error::DatabaseAccessError), Err(_) => Err(Error::DatabaseAccessError),
} }
@ -221,7 +222,7 @@ async fn _post_comment(
Ok(page_data_option) => match page_data_option { Ok(page_data_option) => match page_data_option {
Some(page_data) => { Some(page_data) => {
if page_data.content_id != comment.content_id { if page_data.content_id != comment.content_id {
return Err(Error::InvalidContentId) return Err(Error::InvalidContentId);
} }
} }
None => return Err(Error::InvalidUrl), // e.g. 404 None => return Err(Error::InvalidUrl), // e.g. 404
@ -247,7 +248,7 @@ async fn _post_comment(
break; break;
} }
} }
}, }
Err(_) => { Err(_) => {
return Err(Error::DatabaseInternalError); return Err(Error::DatabaseInternalError);
} }

Loading…
Cancel
Save