Skip to content

Commit

Permalink
feat: truncates long subjects
Browse files Browse the repository at this point in the history
complete message still present in the email body
  • Loading branch information
grzuy committed Jul 23, 2024
1 parent 1262586 commit 1165b0b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/tower/email/message.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
defmodule Tower.Email.Message do
@subject_max_length 120

def new(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: "[#{app_name()}][#{environment()}] #{kind}: #{reason}",
subject: truncate(subject(kind, reason), @subject_max_length),
html_body: html_body(kind, reason, stacktrace),
text_body: text_body(kind, reason, stacktrace)
)
end

defp subject(kind, reason) do
"[#{app_name()}][#{environment()}] #{kind}: #{reason}"
end

defp app_name do
Application.fetch_env!(:tower_email, :otp_app)
end
Expand All @@ -17,6 +23,15 @@ defmodule Tower.Email.Message do
Application.fetch_env!(:tower_email, :environment)
end

defp truncate(text, max_length) do
if String.length(text) <= max_length do
text
else
suffix = "..."
"#{String.slice(text, 0, max_length - String.length(suffix))}#{suffix}"
end
end

require EEx

EEx.function_from_string(
Expand Down
28 changes: 28 additions & 0 deletions test/tower_email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ defmodule TowerEmailTest do
)
end

@tag capture_log: true
test "reports long match error" do
in_unlinked_process(fn ->
%{eleven: "eleven"} = %{
one: "one",
two: "two",
three: "three",
four: "four",
five: "five",
six: "six",
seven: "seven",
eight: "eight",
nine: "nine",
ten: "ten"
}
end)

# TODO: Support waiting on assert_email_sent with a timeout
# Swoosh.TestAssertions.assert_email_sent(subject: "ArithmeticError: bad argument in arithmetic expression")
assert_receive(
{
:email,
%{subject: ~s([tower_email][test] MatchError: no match of right hand side value: %{one: "one", two: "two", three: "three", four: "f...)}
},
1_000
)
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
{:ok, pid} = Task.Supervisor.start_link()

Expand Down

0 comments on commit 1165b0b

Please sign in to comment.