diff --git a/lib/tower/email/message.ex b/lib/tower/email/message.ex index 86f1261..1ae13c5 100644 --- a/lib/tower/email/message.ex +++ b/lib/tower/email/message.ex @@ -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", "undefined@example.com"}), - 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 @@ -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( diff --git a/test/tower_email_test.exs b/test/tower_email_test.exs index 91257a3..3cea989 100644 --- a/test/tower_email_test.exs +++ b/test/tower_email_test.exs @@ -30,6 +30,37 @@ 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()