From b69a78e027d8fa5b98823bd8fbc71a33ffbdc648 Mon Sep 17 00:00:00 2001 From: dmcclung <35938+dmcclung@users.noreply.github.com> Date: Fri, 12 Apr 2024 00:18:33 -0500 Subject: [PATCH] feat(newsletter): publish error itype is more specific --- src/routes/newsletter.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/routes/newsletter.rs b/src/routes/newsletter.rs index f95682e..22a3579 100644 --- a/src/routes/newsletter.rs +++ b/src/routes/newsletter.rs @@ -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}; @@ -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) + } + } + } +}