Skip to content

Add .enabled configuration #10

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

Merged
merged 1 commit into from
Jan 15, 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
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