Skip to content

Commit

Permalink
log mailer errors in ce (#4885)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Jan 6, 2025
1 parent 17c3568 commit e594293
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions lib/plausible/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,21 @@ defmodule Plausible.Mailer do
use Bamboo.Mailer, otp_app: :plausible
require Logger

@type result() :: :ok | {:error, :hard_bounce} | {:error, :unknown_error}

@spec send(Bamboo.Email.t()) :: result()
@spec send(Bamboo.Email.t()) :: :ok | {: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

defp handle_error(%{response: response}) when is_binary(response) do
case Jason.decode(response) do
{:ok, %{"ErrorCode" => 406}} ->
{:error, :hard_bounce}
try do
deliver_now!(email)
rescue
e ->
# this message is ignored by Sentry, only appears in logs
log = "Failed to send e-mail:\n\n " <> Exception.format(:error, e, __STACKTRACE__)
# Sentry report is built entirely from crash_reason
crash_reason = {e, __STACKTRACE__}

{:ok, response} ->
Logger.error("Failed to send e-mail", sentry: %{extra: %{response: response}})
{:error, :unknown_error}

{:error, _any} ->
Logger.error("Failed to send e-mail", sentry: %{extra: %{response: response}})
Logger.error(log, crash_reason: crash_reason)
{:error, :unknown_error}
else
_sent_email -> :ok
end
end

defp handle_error(error) do
Logger.error("Failed to send e-mail", sentry: %{extra: %{response: error}})
{:error, :unknown_error}
end
end

0 comments on commit e594293

Please sign in to comment.