Skip to content

Commit bdd39cd

Browse files
committed
Add a simple rake task to test the setup in production
1 parent f916ef3 commit bdd39cd

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ All exceptions are reported automatically. No additional code required.
2828

2929
Please consult the [official guides](https://guides.rubyonrails.org/error_reporting.html) for an introduction to the error reporting API.
3030

31+
## Test your setup
32+
33+
You can use the built-in rake task `rake email_error_reporter:check` to check if everything works correctly in your setup.
34+
35+
If you're using Kamal, you can run the following command to test the setup in production:
36+
37+
```
38+
kamal app exec -p 'bundle exec rake email_error_reporter:test_email'
39+
```
40+
3141
## License
3242

3343
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
# desc "Explaining what the task does"
2-
# task :email_error_reporter do
3-
# # Task goes here
4-
# end
1+
desc "Report a test exception"
2+
namespace :email_error_reporter do
3+
task check: :environment do
4+
Rails.error.handle { raise "This is a test!" }
5+
$stdout.puts <<~TXT
6+
Test exception triggered.
7+
8+
Recipients:
9+
#{Rails.application.config.email_error_reporter.to.join("\n").gsub(/^/, "* ")}
10+
TXT
11+
end
12+
end

test/dummy/config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@
7373

7474
# Raise error when a before_action's only/except options reference missing actions
7575
config.action_controller.raise_on_missing_callback_actions = true
76+
77+
config.email_error_reporter.to = ["[email protected]"]
7678
end

test/rake_task_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "test_helper"
2+
3+
class EmailErrorReporter::ErrorMailerTest < ActionMailer::TestCase
4+
test "renders the template correctly" do
5+
exception = Exception.new("some message")
6+
email = EmailErrorReporter::ErrorMailer.error(
7+
exception,
8+
handled: true,
9+
severity: :info,
10+
context: {}
11+
)
12+
assert_equal [], email.to
13+
assert_equal "Exception", email.subject
14+
assert_equal read_fixture("error").join, email.body.to_s
15+
end
16+
17+
test "renders a backtrace" do
18+
exception = Exception.new("some message")
19+
exception.set_backtrace(["foo", "bar"])
20+
email = EmailErrorReporter::ErrorMailer.error(
21+
exception,
22+
handled: true,
23+
severity: :info,
24+
context: {}
25+
)
26+
assert_equal [], email.to
27+
assert_equal "Exception", email.subject
28+
assert_equal read_fixture("error_with_backtrace").join, email.body.to_s
29+
end
30+
end

0 commit comments

Comments
 (0)