From e9d4b14dcf0572051c94185fd9172fa722038dbc Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Wed, 28 Jul 2021 09:35:34 +0100 Subject: [PATCH] Add error. --- src/routes/newsletters.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/routes/newsletters.rs b/src/routes/newsletters.rs index 0b925a3..b62e25e 100644 --- a/src/routes/newsletters.rs +++ b/src/routes/newsletters.rs @@ -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) -> 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, + pool: web::Data, +) -> Result { + let subscribers = get_confirmed_subscribers(&pool).await?; + Ok(HttpResponse::Ok().finish()) } struct ConfirmedSubscriber {