Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple email template with all information #4

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ gem "sqlite3"

# Start debugger with binding.b [https://github.com/ruby/debug]
gem "debug", ">= 1.0.0"

gem "rails-dom-testing"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<tr>
<td><%= frame %></td>
<td style="font-family: monospace; padding: 2px;"><%= frame %></td>
</tr>
30 changes: 22 additions & 8 deletions app/views/email_error_reporter/error_mailer/error.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
<h1><%= @error.class %></h1>
<p><%= @error.message %></p>

<div>
<table border="1">
<%= render(partial: "frame", collection: @backtrace) || render("no_backtrace") -%>
</table>
</div>
<html>
<body style="max-width: 768px; margin: 0 auto;">
<h1 style="margin-top: 12px; font-family: sans-serif; font-size: 19px;"><%= @error.class %></h1>
<h2 style="font-family: sans-serif; font-size: 15px;"><%= @error.message %></h2>

<p data-test-id="source">Source: <%= @source ? @source : "No source present" %></p>
<p data-test-id="handled">Handled: <%= @handled ? "✅" : "❌" %></p>

<details style="margin-bottom: 12px;">
<summary>Stacktrace</summary>

<table data-test-id="backtrace" border="1" width="100%" style="margin-top: 12px; margin-bottom: 12px; border-collapse: collapse; border-color: #000000">
<%= render(partial: "frame", collection: @backtrace) || render("no_backtrace") -%>
</table>
</details>

<details>
<summary>Context</summary>
<pre data-test-id="context" style="font-family: mono; background: #eeeeee;"><code><%= @context %></code></pre>
</details>
</body>
</html>
1 change: 1 addition & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.action_mailer.default_options = { from: '[email protected]' }
end
end
2 changes: 2 additions & 0 deletions test/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@

# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true

config.email_error_reporter.to = ["[email protected]"]
end
31 changes: 19 additions & 12 deletions test/error_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ class EmailErrorReporter::ErrorMailerTest < ActionMailer::TestCase
@exception = Exception.new("some message")
end

test "renders the template correctly" do
assert_equal [], error_mail.to
assert_equal read_fixture("error").join, error_mail.body.to_s
end

test "renders a backtrace" do
@exception.set_backtrace(["foo", "bar"])

assert_equal [], error_mail.to
assert_equal read_fixture("error_with_backtrace").join, error_mail.body.to_s
end

test "severity: error" do
email = error_mail(severity: :error)
assert_equal "🔥" + " Exception", email.subject
Expand All @@ -32,6 +20,21 @@ class EmailErrorReporter::ErrorMailerTest < ActionMailer::TestCase
assert_equal "ℹ️" + " Exception", email.subject
end

test "email content" do
@exception.set_backtrace(["foo", "bar"])
email = error_mail(handled: true, severity: :info).deliver_now
assert_dom_email do
assert_dom "h1", "Exception"
assert_dom "h2", "some message"
assert_dom test_id("source"), "Source: No source present"
assert_dom test_id("handled"), "Handled: ✅"
assert_dom test_id("context"), "{}"
assert_dom "table" do
assert_dom "tr", 2
end
end
end

private

def error_mail(**kwargs)
Expand All @@ -43,4 +46,8 @@ def error_mail(**kwargs)
**kwargs
)
end

def test_id(value)
"[data-test-id='#{value}']"
end
end