Skip to content

Commit

Permalink
feat(newsletter): publish error itype is more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcclung committed Apr 13, 2024
1 parent 5778330 commit b69a78e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/routes/newsletter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! src/routes/newsletter.rs

use std::sync::Arc;
use std::{
fmt::{Display, Error, Formatter},
sync::Arc,
};

use actix_web::{web, HttpResponse};
use actix_web::{web, HttpResponse, ResponseError};
use serde::Deserialize;
use sqlx::{Pool, Postgres};
use tracing::{info, instrument, Instrument};
Expand Down Expand Up @@ -67,3 +70,31 @@ pub async fn publish_newsletter(

Ok(HttpResponse::Ok().finish())
}

#[derive(Debug)]
pub enum PublishError {
DatabaseError(sqlx::Error),
EmailError(String),
}

impl Display for PublishError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
PublishError::DatabaseError(e) => write!(f, "Database Error: {}", e),
PublishError::EmailError(e) => write!(f, "Error sending email: {}", e),
}
}
}

impl ResponseError for PublishError {
fn error_response(&self) -> HttpResponse {
match self {
PublishError::DatabaseError(ref error) => {
HttpResponse::InternalServerError().json(error.to_string())
}
PublishError::EmailError(ref message) => {
HttpResponse::InternalServerError().json(message)
}
}
}
}

0 comments on commit b69a78e

Please sign in to comment.