Skip to content

Commit 4b99a97

Browse files
authored
Add .enabled configuration (#10)
1 parent 46d7b7e commit 4b99a97

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Diff for: README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Add the gem:
1111

1212
```bash
13-
bundle add email_error_reporter && bundle install
13+
bundle add email_error_reporter
1414
```
1515

1616
and configure the email addresses that should receive an email in the case of an exception:
@@ -37,6 +37,14 @@ Set a custom from address.
3737
config.email_error_reporter.from = "[email protected]"
3838
```
3939

40+
Disables the email reports for specific environments. Mails are enabled by default on all envs.
41+
42+
```ruby
43+
# e.g. in development.rb
44+
# default: true
45+
config.email_error_reporter.enabled = false
46+
```
47+
4048
## Test your setup
4149

4250
You can use the built-in rake task `rake email_error_reporter:check` to check if everything works correctly in your setup.

Diff for: lib/email_error_reporter/engine.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ class Engine < ::Rails::Engine
55
config.email_error_reporter = ActiveSupport::OrderedOptions.new
66
config.email_error_reporter.to = []
77
config.email_error_reporter.from = "[email protected]"
8+
config.email_error_reporter.enabled = true
89

9-
initializer "email_error_reporter.error_subscribe" do
10-
Rails.error.subscribe(Subscriber.new)
10+
initializer "email_error_reporter.error_subscribe" do |app|
11+
if app.config.email_error_reporter.enabled
12+
Rails.error.subscribe(Subscriber.new)
13+
end
1114
end
1215

1316
# ActiveJob cannot (de)-serialize exceptions by default

Diff for: test/subscriber_test.rb

+25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ class SubscriberTest < ActiveSupport::TestCase
2626
)
2727
end
2828

29+
test "enqueues no mail if disabled" do
30+
old_config = Rails.application.config.email_error_reporter.enabled
31+
Rails.application.config.email_error_reporter.enabled = false
32+
Rails.error.record(TestError) do
33+
raise TestError
34+
end
35+
36+
rescue TestError => e
37+
# matcher
38+
assert_enqueued_with(
39+
job: ActionMailer::MailDeliveryJob,
40+
args: ->(j) {
41+
[
42+
"EmailErrorReporter::ErrorMailer" == j[0],
43+
"error" == j[1],
44+
"deliver_now" == j[2],
45+
e.class == j[3][:args][0].class,
46+
{ handled: false, context: {}, severity: :error, source: rails_default_source } == j[3][:args][1],
47+
].all?
48+
}
49+
)
50+
ensure
51+
Rails.application.config.email_error_reporter.enabled = old_config
52+
end
53+
2954
private
3055

3156
def rails_default_source

0 commit comments

Comments
 (0)