From ffc49a83adcccfbcce64c061a6e0a7d0414f0546 Mon Sep 17 00:00:00 2001 From: Tsvetozar Penov Date: Thu, 21 Nov 2024 13:24:52 +0100 Subject: [PATCH] Catch json decode error email response --- lib/sanbase/alerts/alert.ex | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/sanbase/alerts/alert.ex b/lib/sanbase/alerts/alert.ex index 3ce4dc2a5..6e899c1e4 100644 --- a/lib/sanbase/alerts/alert.ex +++ b/lib/sanbase/alerts/alert.ex @@ -385,14 +385,19 @@ defimpl Sanbase.Alert, for: Any do payload_html = Earmark.as_html!(payload, breaks: true, timeout: nil, mapper: &Enum.map/2) name = Sanbase.Accounts.User.get_name(user) - Sanbase.TemplateMailer.send(user.email, Sanbase.Email.Template.alerts_template(), %{ - name: name, - username: name, - payload: payload_html - }) - |> case do - {:ok, _} -> :ok - {:error, reason} -> {:error, reason} + try do + case Sanbase.TemplateMailer.send(user.email, Sanbase.Email.Template.alerts_template(), %{ + name: name, + username: name, + payload: payload_html + }) do + {:ok, _} -> :ok + {:error, reason} -> {:error, %{reason: :email_send_fail, error: reason}} + end + rescue + e in Jason.DecodeError -> + Logger.error("Failed to decode Mailjet response: #{inspect(e)}") + {:error, "Invalid response from email provider"} end end