Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add original email address to failed delivery error #104

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ async fn catch_claims_in_db_fn() -> Result<()> {
email_addr,
error_subject: "Voiding claim".to_string(),
error: err.to_string(),
email_headers: None,
}
}
};
Expand Down
20 changes: 16 additions & 4 deletions packages/relayer/src/modules/mail.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::*;
use handlebars::Handlebars;
use relayer_utils::AccountCode;
use relayer_utils::{AccountCode, EmailHeaders};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -48,6 +48,7 @@ pub enum EmailWalletEvent {
email_addr: String,
error_subject: String,
error: String,
email_headers: Option<EmailHeaders>,
},
Ack {
email_addr: String,
Expand Down Expand Up @@ -208,7 +209,6 @@ pub async fn handle_email_event(event: EmailWalletEvent) -> Result<()> {
email_addr, CHAIN_RPC_EXPLORER.get().unwrap(), wallet_addr,
);
let render_data = serde_json::json!({"userEmailAddr": email_addr, "walletAddr": wallet_addr, "assetsList": assets_list_html, "chainRPCExplorer": CHAIN_RPC_EXPLORER.get().unwrap()});
println!("render_data: {:?}", render_data);
let body_html = render_html("invitation.html", render_data).await?;
let email = EmailMessage {
to: email_addr.to_string(),
Expand Down Expand Up @@ -301,8 +301,9 @@ pub async fn handle_email_event(event: EmailWalletEvent) -> Result<()> {
email_addr,
error_subject,
error,
email_headers,
} => {
let error = parse_error(error)?;
let error = parse_error(error, email_headers)?;
if let Some(error) = error {
let subject = format!("Email Wallet Notification. Error occurred.");
let body_plain = format!("Hi {}!\nError occurred: {}", email_addr, error);
Expand Down Expand Up @@ -379,7 +380,7 @@ pub async fn render_html(template_name: &str, render_data: Value) -> Result<Stri
Ok(reg.render_template(&email_template, &render_data)?)
}

pub fn parse_error(error: String) -> Result<Option<String>> {
pub fn parse_error(error: String, email_headers: Option<EmailHeaders>) -> Result<Option<String>> {
let mut error = error;
if error.contains("Contract call reverted with data: ") {
let revert_data = error
Expand All @@ -398,6 +399,17 @@ pub fn parse_error(error: String) -> Result<Option<String>> {
match error.as_str() {
"Account is already created" => Ok(Some(error)),
"insufficient balance" => Ok(Some("You don't have sufficient balance".to_string())),
"The user of email address [email protected] is not registered." => {
let failed_recipients = email_headers
.and_then(|headers| headers.get_header("X-Failed-Recipients"))
.and_then(|v| v.first().cloned())
.unwrap_or_else(|| "unknown recipient".to_string());

Ok(Some(format!(
"Failed to deliver email to {}. The address does not exist.",
failed_recipients
)))
}
_ => Ok(Some(error)),
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/relayer/src/modules/web_server/rest_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ pub async fn receive_email_api_fn(email: String) -> Result<()> {
email_addr: from_addr,
error_subject: parsed_email.get_subject_all().unwrap_or_default(),
error: e.to_string(),
email_headers: Some(parsed_email.headers.clone()),
})
.await
{
Expand Down