Skip to content

Commit

Permalink
feat: includes event id in message body
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Aug 5, 2024
1 parent c279120 commit 7005a84
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
15 changes: 10 additions & 5 deletions lib/tower/email/message.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Tower.Email.Message do
@subject_max_length 120

def new(kind, reason, stacktrace \\ nil) do
def new(id, kind, reason, stacktrace \\ nil) do
Swoosh.Email.new(
to: Application.fetch_env!(:tower_email, :to),
from: Application.get_env(:tower_email, :from, {"Undefined From", "[email protected]"}),
subject: truncate(subject(kind, reason), @subject_max_length),
html_body: html_body(kind, reason, stacktrace),
text_body: text_body(kind, reason, stacktrace)
html_body: html_body(id, kind, reason, stacktrace),
text_body: text_body(id, kind, reason, stacktrace)
)
end

Expand Down Expand Up @@ -55,8 +55,12 @@ defmodule Tower.Email.Message do
<% end %>
</code>
<% end %>
<p>
ID: <%= id %>
</p>
""",
[:kind, :reason, :stacktrace]
[:id, :kind, :reason, :stacktrace]
)

EEx.function_from_string(
Expand All @@ -68,7 +72,8 @@ defmodule Tower.Email.Message do
<%= if stacktrace do %>
<%= Exception.format_stacktrace(stacktrace) %>
<% end %>
id: <%= id %>
""",
[:kind, :reason, :stacktrace]
[:id, :kind, :reason, :stacktrace]
)
end
20 changes: 10 additions & 10 deletions lib/tower/email/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ defmodule Tower.Email.Reporter do
@behaviour Tower.Reporter

@impl true
def report_event(%Tower.Event{kind: :error, reason: exception, stacktrace: stacktrace}) do
send_email(inspect(exception.__struct__), Exception.message(exception), stacktrace)
def report_event(%Tower.Event{kind: :error, id: id, reason: exception, stacktrace: stacktrace}) do
send_email(id, inspect(exception.__struct__), Exception.message(exception), stacktrace)
end

def report_event(%Tower.Event{kind: :throw, reason: reason, stacktrace: stacktrace}) do
send_email("Uncaught throw", reason, stacktrace)
def report_event(%Tower.Event{kind: :throw, id: id, reason: reason, stacktrace: stacktrace}) do
send_email(id, "Uncaught throw", reason, stacktrace)
end

def report_event(%Tower.Event{kind: :exit, reason: reason, stacktrace: stacktrace}) do
send_email("Exit", reason, stacktrace)
def report_event(%Tower.Event{kind: :exit, id: id, reason: reason, stacktrace: stacktrace}) do
send_email(id, "Exit", reason, stacktrace)
end

def report_event(%Tower.Event{kind: :message, level: level, reason: message}) do
def report_event(%Tower.Event{kind: :message, id: id, level: level, reason: message}) do
m =
if is_binary(message) do
message
else
inspect(message)
end

send_email("[#{level}] #{m}", "")
send_email(id, "[#{level}] #{m}", "")
end

defp send_email(kind, reason, stacktrace \\ nil) do
defp send_email(id, kind, reason, stacktrace \\ nil) do
{:ok, _} =
Tower.Email.Message.new(kind, reason, stacktrace)
Tower.Email.Message.new(id, kind, reason, stacktrace)
|> Tower.Email.Mailer.deliver()

:ok
Expand Down

0 comments on commit 7005a84

Please sign in to comment.