Add error.

This commit is contained in:
LukeMathWalker 2021-07-28 09:35:34 +01:00
parent 5b87b2449d
commit e9d4b14dcf

View file

@ -1,5 +1,7 @@
use actix_web::{web, HttpResponse};
use actix_web::{web, HttpResponse, ResponseError};
use sqlx::PgPool;
use crate::routes::error_chain_fmt;
use actix_web::http::StatusCode;
#[derive(serde::Deserialize)]
pub struct BodyData {
@ -13,8 +15,32 @@ pub struct Content {
text: String,
}
pub async fn publish_newsletter(_body: web::Json<BodyData>) -> HttpResponse {
HttpResponse::Ok().finish()
#[derive(thiserror::Error)]
pub enum PublishError {
#[error(transparent)]
UnexpectedError(#[from] anyhow::Error),
}
impl std::fmt::Debug for PublishError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
error_chain_fmt(self, f)
}
}
impl ResponseError for PublishError {
fn status_code(&self) -> StatusCode {
match self {
PublishError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
pub async fn publish_newsletter(
body: web::Json<BodyData>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, PublishError> {
let subscribers = get_confirmed_subscribers(&pool).await?;
Ok(HttpResponse::Ok().finish())
}
struct ConfirmedSubscriber {