diff --git a/README.md b/README.md index 8fed9e0..f43e6bc 100644 --- a/README.md +++ b/README.md @@ -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: @@ -37,6 +37,14 @@ Set a custom from address. config.email_error_reporter.from = "your-custom-email@example.com" ``` +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. diff --git a/lib/email_error_reporter/engine.rb b/lib/email_error_reporter/engine.rb index b0d84c2..d6a2ea4 100644 --- a/lib/email_error_reporter/engine.rb +++ b/lib/email_error_reporter/engine.rb @@ -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 = "no-reply@example.com" + 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 diff --git a/test/subscriber_test.rb b/test/subscriber_test.rb index 27b4016..ce3df2c 100644 --- a/test/subscriber_test.rb +++ b/test/subscriber_test.rb @@ -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