mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 05:56:35 +00:00
Add error.
This commit is contained in:
parent
5b87b2449d
commit
e9d4b14dcf
1 changed files with 29 additions and 3 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue