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 case Repo.update(changeset) do
{:ok, updated} -> {:ok, updated} ->
PlausibleWeb.Email.cancellation_email(subscription.user) PlausibleWeb.Email.cancellation_email(subscription.user)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
{:ok, updated} {:ok, updated}

View File

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

View File

@ -1,29 +1,32 @@
defmodule Plausible.Mailer do defmodule Plausible.Mailer do
use Bamboo.Mailer, otp_app: :plausible use Bamboo.Mailer, otp_app: :plausible
def send_email(email) do @spec send(Bamboo.Email.t()) :: :ok | {:error, :hard_bounce} | {:error, :unknown_error}
try do def send(email) do
Plausible.Mailer.deliver_now!(email) case deliver_now(email) do
rescue {:ok, _email} -> :ok
error -> {:ok, _email, _response} -> :ok
Sentry.capture_exception(error, {:error, error} -> handle_error(error)
stacktrace: __STACKTRACE__,
extra: %{extra: "Error while sending email"}
)
reraise error, __STACKTRACE__
end end
end end
def send_email_safe(email) do defp handle_error(%{response: response}) when is_binary(response) do
try do case Jason.decode(response) do
Plausible.Mailer.deliver_now!(email) {:ok, %{"ErrorCode" => 406}} ->
rescue {:error, :hard_bounce}
error ->
Sentry.capture_exception(error, {:ok, response} ->
stacktrace: __STACKTRACE__, Sentry.capture_message("Failed to send e-mail", extra: %{response: response})
extra: %{extra: "Error while sending email"} {:error, :unknown_error}
)
{:error, _any} ->
Sentry.capture_message("Failed to send e-mail", extra: %{response: response})
{:error, :unknown_error}
end end
end end
defp handle_error(error) do
Sentry.capture_message("Failed to send e-mail", extra: %{response: error})
{:error, :unknown_error}
end
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 defp send_email_verification(user) do
code = Auth.issue_email_verification(user) code = Auth.issue_email_verification(user)
email_template = PlausibleWeb.Email.activation_email(user, code) email_template = PlausibleWeb.Email.activation_email(user, code)
result = Plausible.Mailer.send_email(email_template) result = Plausible.Mailer.send(email_template)
Logger.debug( Logger.debug(
"E-mail verification e-mail sent. In dev environment GET /sent-emails for details." "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) code = Auth.issue_email_verification(user)
email_template = PlausibleWeb.Email.activation_email(user, code) email_template = PlausibleWeb.Email.activation_email(user, code)
Plausible.Mailer.send_email(email_template) Plausible.Mailer.send(email_template)
conn conn
|> put_flash(:success, "Activation code was sent to #{user.email}") |> 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 defp notify_invitation_accepted(%Invitation{role: :owner} = invitation) do
PlausibleWeb.Email.ownership_transfer_accepted(invitation) PlausibleWeb.Email.ownership_transfer_accepted(invitation)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
end end
defp notify_invitation_accepted(invitation) do defp notify_invitation_accepted(invitation) do
PlausibleWeb.Email.invitation_accepted(invitation) PlausibleWeb.Email.invitation_accepted(invitation)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
end end
defp notify_invitation_rejected(%Invitation{role: :owner} = invitation) do defp notify_invitation_rejected(%Invitation{role: :owner} = invitation) do
PlausibleWeb.Email.ownership_transfer_rejected(invitation) PlausibleWeb.Email.ownership_transfer_rejected(invitation)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
end end
defp notify_invitation_rejected(invitation) do defp notify_invitation_rejected(invitation) do
PlausibleWeb.Email.invitation_rejected(invitation) PlausibleWeb.Email.invitation_rejected(invitation)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
end end
def remove_invitation(conn, %{"invitation_id" => invitation_id}) do 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) PlausibleWeb.Email.new_user_invitation(invitation)
end end
Plausible.Mailer.send_email(email_template) Plausible.Mailer.send(email_template)
conn conn
|> put_flash( |> put_flash(
@ -124,7 +124,7 @@ defmodule PlausibleWeb.Site.MembershipController do
|> Repo.preload([:site, :inviter]) |> Repo.preload([:site, :inviter])
PlausibleWeb.Email.ownership_transfer_request(invitation, user) PlausibleWeb.Email.ownership_transfer_request(invitation, user)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
conn conn
|> put_flash(:success, "Site transfer request has been sent to #{email}") |> put_flash(:success, "Site transfer request has been sent to #{email}")
@ -199,7 +199,7 @@ defmodule PlausibleWeb.Site.MembershipController do
Repo.delete!(membership) Repo.delete!(membership)
PlausibleWeb.Email.site_member_removed(membership) PlausibleWeb.Email.site_member_removed(membership)
|> Plausible.Mailer.send_email() |> Plausible.Mailer.send()
redirect_target = redirect_target =
if membership.user.id == conn.assigns[:current_user].id do if membership.user.id == conn.assigns[:current_user].id do

View File

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

View File

@ -79,7 +79,7 @@ defmodule Plausible.Workers.CheckUsage do
site_allowance site_allowance
) )
Plausible.Mailer.send_email_safe(template) Plausible.Mailer.send(template)
subscriber subscriber
|> Plausible.Auth.GracePeriod.start_manual_lock_changeset(last_cycle_usage) |> Plausible.Auth.GracePeriod.start_manual_lock_changeset(last_cycle_usage)
@ -100,7 +100,7 @@ defmodule Plausible.Workers.CheckUsage do
suggested_plan suggested_plan
) )
Plausible.Mailer.send_email_safe(template) Plausible.Mailer.send(template)
subscriber subscriber
|> Plausible.Auth.GracePeriod.start_changeset(last_cycle_usage) |> 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 -> Enum.each(site.memberships, fn membership ->
if membership.role in [:owner, :admin] do if membership.role in [:owner, :admin] do
PlausibleWeb.Email.import_success(membership.user, site) PlausibleWeb.Email.import_success(membership.user, site)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
end end
end) end)
@ -62,7 +62,7 @@ defmodule Plausible.Workers.ImportGoogleAnalytics do
Enum.each(site.memberships, fn membership -> Enum.each(site.memberships, fn membership ->
if membership.role in [:owner, :admin] do if membership.role in [:owner, :admin] do
PlausibleWeb.Email.import_failure(membership.user, site) PlausibleWeb.Email.import_failure(membership.user, site)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
end end
end) end)
end end

View File

@ -46,11 +46,11 @@ defmodule Plausible.Workers.NotifyAnnualRenewal do
case user.subscription.status do case user.subscription.status do
"active" -> "active" ->
template = PlausibleWeb.Email.yearly_renewal_notification(user) template = PlausibleWeb.Email.yearly_renewal_notification(user)
Plausible.Mailer.send_email_safe(template) Plausible.Mailer.send(template)
"deleted" -> "deleted" ->
template = PlausibleWeb.Email.yearly_expiration_notification(user) 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) 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 defp send_check_stats_email(user) do
PlausibleWeb.Email.check_stats_email(user) PlausibleWeb.Email.check_stats_email(user)
|> Plausible.Mailer.send_email_safe() |> Plausible.Mailer.send()
Repo.insert_all("check_stats_emails", [ Repo.insert_all("check_stats_emails", [
%{ %{

View File

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

View File

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

View File

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

View File

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