Handler to send emails to confirmed subscribers
This commit is contained in:
@@ -24,7 +24,10 @@ fn generate_subscription_token() -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn error_chain_fmt(e: &impl std::error::Error, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
pub fn error_chain_fmt(
|
||||
e: &impl std::error::Error,
|
||||
f: &mut std::fmt::Formatter,
|
||||
) -> std::fmt::Result {
|
||||
writeln!(f, "{}", e)?;
|
||||
let mut current = e.source();
|
||||
while let Some(cause) = current {
|
||||
@@ -37,24 +40,12 @@ fn error_chain_fmt(e: &impl std::error::Error, f: &mut std::fmt::Formatter) -> s
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// #[derive(thiserror::Error)]
|
||||
// pub enum SubscribeError {
|
||||
// #[error("Failed to store the confirmation token for a new subscriber.")]
|
||||
// StoreToken(#[from] StoreTokenError),
|
||||
// #[error("A database error occured.")]
|
||||
// Database(#[from] sqlx::Error),
|
||||
// #[error("Failed to send a confirmation email.")]
|
||||
// SendEmail(#[from] reqwest::Error),
|
||||
// #[error("{0}")]
|
||||
// Validation(String),
|
||||
// }
|
||||
|
||||
#[derive(thiserror::Error)]
|
||||
pub enum SubscribeError {
|
||||
#[error(transparent)]
|
||||
UnexpectedError(#[from] anyhow::Error),
|
||||
#[error("{0}")]
|
||||
Validation(String),
|
||||
ValidationError(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for SubscribeError {
|
||||
@@ -72,15 +63,9 @@ impl IntoResponse for SubscribeError {
|
||||
|
||||
tracing::error!("{:?}", self);
|
||||
|
||||
// let status = match self {
|
||||
// SubscribeError::StoreToken(_)
|
||||
// | SubscribeError::Database(_)
|
||||
// | SubscribeError::SendEmail(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
// SubscribeError::Validation(_) => StatusCode::BAD_REQUEST,
|
||||
// };
|
||||
let status = match self {
|
||||
SubscribeError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
SubscribeError::Validation(_) => StatusCode::BAD_REQUEST,
|
||||
SubscribeError::ValidationError(_) => StatusCode::BAD_REQUEST,
|
||||
};
|
||||
let message = "An internal server error occured.";
|
||||
(status, Json(ErrorResponse { message })).into_response()
|
||||
@@ -107,7 +92,7 @@ pub async fn subscribe(
|
||||
.begin()
|
||||
.await
|
||||
.context("Failed to acquire a Postgres connection from the pool.")?;
|
||||
let new_subscriber = form.try_into().map_err(SubscribeError::Validation)?;
|
||||
let new_subscriber = form.try_into().map_err(SubscribeError::ValidationError)?;
|
||||
let subscriber_id = insert_subscriber(&mut transaction, &new_subscriber)
|
||||
.await
|
||||
.context("Failed to insert new subscriber in the database.")?;
|
||||
|
||||
Reference in New Issue
Block a user