main
Elnu 2 years ago
parent f68f2155c3
commit 6de079b279

@ -37,19 +37,20 @@ enum Error {
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
| 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",
@ -62,15 +63,13 @@ impl Error {
Self::DatabaseInternalError => "database internal error",
Self::SanitizationError => "comment sanitization error",
Self::PageFetchError => "page fetch error",
}).finish()
})
.finish()
}
}
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 {
Some(origin) => origin,
None => return Err(Error::InvalidOrigin),
@ -141,7 +140,9 @@ async fn _get_comments(
Err(_) => return Err(Error::DatabaseInternalError),
},
)
}).await {
})
.await
{
Ok(result) => result,
Err(_) => Err(Error::DatabaseAccessError),
}
@ -221,7 +222,7 @@ async fn _post_comment(
Ok(page_data_option) => match page_data_option {
Some(page_data) => {
if page_data.content_id != comment.content_id {
return Err(Error::InvalidContentId)
return Err(Error::InvalidContentId);
}
}
None => return Err(Error::InvalidUrl), // e.g. 404
@ -247,7 +248,7 @@ async fn _post_comment(
break;
}
}
},
}
Err(_) => {
return Err(Error::DatabaseInternalError);
}

Loading…
Cancel
Save