Do not report hard bounce e-mail errors to Sentry (#2361)

This commit is contained in:
Vinicius Brasil 2022-10-24 07:13:23 -03:00 committed by GitHub
parent 0fa6b688af
commit 9a61a10273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 63 additions and 48 deletions

View File

@ -69,7 +69,7 @@ defmodule Plausible.Billing do
case Repo.update(changeset) do
{:ok, updated} ->
PlausibleWeb.Email.cancellation_email(subscription.user)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
{:ok, updated}

View File

@ -50,6 +50,6 @@ defmodule Plausible.Billing.SiteLocker do
suggested_plan
)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
end
end

View File

@ -1,29 +1,32 @@
defmodule Plausible.Mailer do
use Bamboo.Mailer, otp_app: :plausible
def send_email(email) do
try do
Plausible.Mailer.deliver_now!(email)
rescue
error ->
Sentry.capture_exception(error,
stacktrace: __STACKTRACE__,
extra: %{extra: "Error while sending email"}
)
reraise error, __STACKTRACE__
@spec send(Bamboo.Email.t()) :: :ok | {:error, :hard_bounce} | {:error, :unknown_error}
def send(email) do
case deliver_now(email) do
{:ok, _email} -> :ok
{:ok, _email, _response} -> :ok
{:error, error} -> handle_error(error)
end
end
def send_email_safe(email) do
try do
Plausible.Mailer.deliver_now!(email)
rescue
error ->
Sentry.capture_exception(error,
stacktrace: __STACKTRACE__,
extra: %{extra: "Error while sending email"}
)
defp handle_error(%{response: response}) when is_binary(response) do
case Jason.decode(response) do
{:ok, %{"ErrorCode" => 406}} ->
{:error, :hard_bounce}
{:ok, response} ->
Sentry.capture_message("Failed to send e-mail", extra: %{response: response})
{:error, :unknown_error}
{:error, _any} ->
Sentry.capture_message("Failed to send e-mail", extra: %{response: response})
{:error, :unknown_error}
end
end
defp handle_error(error) do
Sentry.capture_message("Failed to send e-mail", extra: %{response: error})
{:error, :unknown_error}
end
end

View File

@ -0,0 +1,12 @@
defmodule Plausible.MailerTest do
use Plausible.DataCase
use Bamboo.Test
test "send/1 sends an email" do
user = build(:user)
email = PlausibleWeb.Email.welcome_email(user)
assert :ok == Plausible.Mailer.send(email)
assert_delivered_email(email)
end
end

View File

@ -146,7 +146,7 @@ defmodule PlausibleWeb.AuthController do
defp send_email_verification(user) do
code = Auth.issue_email_verification(user)
email_template = PlausibleWeb.Email.activation_email(user, code)
result = Plausible.Mailer.send_email(email_template)
result = Plausible.Mailer.send(email_template)
Logger.debug(
"E-mail verification e-mail sent. In dev environment GET /sent-emails for details."
@ -228,7 +228,7 @@ defmodule PlausibleWeb.AuthController do
code = Auth.issue_email_verification(user)
email_template = PlausibleWeb.Email.activation_email(user, code)
Plausible.Mailer.send_email(email_template)
Plausible.Mailer.send(email_template)
conn
|> put_flash(:success, "Activation code was sent to #{user.email}")

View File

@ -91,22 +91,22 @@ defmodule PlausibleWeb.InvitationController do
defp notify_invitation_accepted(%Invitation{role: :owner} = invitation) do
PlausibleWeb.Email.ownership_transfer_accepted(invitation)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
defp notify_invitation_accepted(invitation) do
PlausibleWeb.Email.invitation_accepted(invitation)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
defp notify_invitation_rejected(%Invitation{role: :owner} = invitation) do
PlausibleWeb.Email.ownership_transfer_rejected(invitation)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
defp notify_invitation_rejected(invitation) do
PlausibleWeb.Email.invitation_rejected(invitation)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
def remove_invitation(conn, %{"invitation_id" => invitation_id}) do

View File

@ -68,7 +68,7 @@ defmodule PlausibleWeb.Site.MembershipController do
PlausibleWeb.Email.new_user_invitation(invitation)
end
Plausible.Mailer.send_email(email_template)
Plausible.Mailer.send(email_template)
conn
|> put_flash(
@ -124,7 +124,7 @@ defmodule PlausibleWeb.Site.MembershipController do
|> Repo.preload([:site, :inviter])
PlausibleWeb.Email.ownership_transfer_request(invitation, user)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
conn
|> put_flash(:success, "Site transfer request has been sent to #{email}")
@ -199,7 +199,7 @@ defmodule PlausibleWeb.Site.MembershipController do
Repo.delete!(membership)
PlausibleWeb.Email.site_member_removed(membership)
|> Plausible.Mailer.send_email()
|> Plausible.Mailer.send()
redirect_target =
if membership.user.id == conn.assigns[:current_user].id do

View File

@ -77,7 +77,7 @@ defmodule PlausibleWeb.SiteController do
{:ok, %{site: site}} ->
if is_first_site do
PlausibleWeb.Email.welcome_email(user)
|> Plausible.Mailer.send_email()
|> Plausible.Mailer.send()
end
conn

View File

@ -79,7 +79,7 @@ defmodule Plausible.Workers.CheckUsage do
site_allowance
)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
subscriber
|> Plausible.Auth.GracePeriod.start_manual_lock_changeset(last_cycle_usage)
@ -100,7 +100,7 @@ defmodule Plausible.Workers.CheckUsage do
suggested_plan
)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
subscriber
|> Plausible.Auth.GracePeriod.start_changeset(last_cycle_usage)

View File

@ -34,7 +34,7 @@ defmodule Plausible.Workers.ImportGoogleAnalytics do
Enum.each(site.memberships, fn membership ->
if membership.role in [:owner, :admin] do
PlausibleWeb.Email.import_success(membership.user, site)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
end)
@ -62,7 +62,7 @@ defmodule Plausible.Workers.ImportGoogleAnalytics do
Enum.each(site.memberships, fn membership ->
if membership.role in [:owner, :admin] do
PlausibleWeb.Email.import_failure(membership.user, site)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
end)
end

View File

@ -46,11 +46,11 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
case user.subscription.status do
"active" ->
template = PlausibleWeb.Email.yearly_renewal_notification(user)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
"deleted" ->
template = PlausibleWeb.Email.yearly_expiration_notification(user)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
_ ->
Sentry.capture_message("Invalid subscription for renewal", user: user)

View File

@ -29,7 +29,7 @@ defmodule Plausible.Workers.SendCheckStatsEmails do
defp send_check_stats_email(user) do
PlausibleWeb.Email.check_stats_email(user)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
Repo.insert_all("check_stats_emails", [
%{

View File

@ -79,6 +79,6 @@ defmodule Plausible.Workers.SendEmailReport do
name: name
)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
end
end

View File

@ -76,7 +76,7 @@ defmodule Plausible.Workers.SendSiteSetupEmails do
defp send_create_site_email(user) do
PlausibleWeb.Email.create_site_email(user)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
Repo.insert_all("create_site_emails", [
%{
@ -88,7 +88,7 @@ defmodule Plausible.Workers.SendSiteSetupEmails do
defp send_setup_success_email(user, site) do
PlausibleWeb.Email.site_setup_success(user, site)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
Repo.insert_all("setup_success_emails", [
%{
@ -100,7 +100,7 @@ defmodule Plausible.Workers.SendSiteSetupEmails do
defp send_setup_help_email(user, site) do
PlausibleWeb.Email.site_setup_help(user, site)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
Repo.insert_all("setup_help_emails", [
%{

View File

@ -51,25 +51,25 @@ defmodule Plausible.Workers.SendTrialNotifications do
defp send_one_week_reminder(user) do
PlausibleWeb.Email.trial_one_week_reminder(user)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
defp send_tomorrow_reminder(user) do
usage = Plausible.Billing.usage_breakdown(user)
PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
defp send_today_reminder(user) do
usage = Plausible.Billing.usage_breakdown(user)
PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
defp send_over_reminder(user) do
PlausibleWeb.Email.trial_over_email(user)
|> Plausible.Mailer.send_email_safe()
|> Plausible.Mailer.send()
end
end

View File

@ -58,6 +58,6 @@ defmodule Plausible.Workers.SpikeNotifier do
dashboard_link
)
Plausible.Mailer.send_email_safe(template)
Plausible.Mailer.send(template)
end
end