Skip to content

Commit

Permalink
Add .enabled configuration (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
codergeek121 authored Jan 15, 2024
1 parent 46d7b7e commit 4b99a97
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Add the gem:

```bash
bundle add email_error_reporter && bundle install
bundle add email_error_reporter
```

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

Disables the email reports for specific environments. Mails are enabled by default on all envs.

```ruby
# e.g. in development.rb
# default: true
config.email_error_reporter.enabled = false
```

## Test your setup

You can use the built-in rake task `rake email_error_reporter:check` to check if everything works correctly in your setup.
Expand Down
7 changes: 5 additions & 2 deletions lib/email_error_reporter/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ class Engine < ::Rails::Engine
config.email_error_reporter = ActiveSupport::OrderedOptions.new
config.email_error_reporter.to = []
config.email_error_reporter.from = "[email protected]"
config.email_error_reporter.enabled = true

initializer "email_error_reporter.error_subscribe" do
Rails.error.subscribe(Subscriber.new)
initializer "email_error_reporter.error_subscribe" do |app|
if app.config.email_error_reporter.enabled
Rails.error.subscribe(Subscriber.new)
end
end

# ActiveJob cannot (de)-serialize exceptions by default
Expand Down
25 changes: 25 additions & 0 deletions test/subscriber_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ class SubscriberTest < ActiveSupport::TestCase
)
end

test "enqueues no mail if disabled" do
old_config = Rails.application.config.email_error_reporter.enabled
Rails.application.config.email_error_reporter.enabled = false
Rails.error.record(TestError) do
raise TestError
end

rescue TestError => e
# matcher
assert_enqueued_with(
job: ActionMailer::MailDeliveryJob,
args: ->(j) {
[
"EmailErrorReporter::ErrorMailer" == j[0],
"error" == j[1],
"deliver_now" == j[2],
e.class == j[3][:args][0].class,
{ handled: false, context: {}, severity: :error, source: rails_default_source } == j[3][:args][1],
].all?
}
)
ensure
Rails.application.config.email_error_reporter.enabled = old_config
end

private

def rails_default_source
Expand Down

0 comments on commit 4b99a97

Please sign in to comment.