diff --git a/lib/govuk_app_config/govuk_error/configuration.rb b/lib/govuk_app_config/govuk_error/configuration.rb index 01dcacd0..0ed1f601 100644 --- a/lib/govuk_app_config/govuk_error/configuration.rb +++ b/lib/govuk_app_config/govuk_error/configuration.rb @@ -12,16 +12,15 @@ def initialize(_raven_configuration) @data_sync = GovukDataSync.new(ENV["GOVUK_DATA_SYNC_PERIOD"]) self.active_sentry_environments = [] self.data_sync_excluded_exceptions = [] - super.before_send = default_before_send_actions + @before_send_callbacks = [ + ignore_exceptions_based_on_env_and_data_sync, + increment_govuk_statsd_counters, + ] + super.before_send = run_before_send_callbacks end def before_send=(closure) - combined = lambda do |error_or_event, hint| - default_before_send_actions.call(error_or_event, hint) && - closure.call(error_or_event, hint) - end - - super(combined) + @before_send_callbacks.insert(-2, closure) end protected @@ -61,9 +60,12 @@ def increment_govuk_statsd_counters } end - def default_before_send_actions + def run_before_send_callbacks lambda do |error_or_event, hint| - (ignore_exceptions_based_on_env_and_data_sync.call(error_or_event, hint) && increment_govuk_statsd_counters.call(error_or_event, hint)) + @before_send_callbacks.each do |callback| + break if callback.call(error_or_event, hint).nil? + end + error_or_event end end end diff --git a/spec/govuk_error/configuration_spec.rb b/spec/govuk_error/configuration_spec.rb index c8b9de4e..5e9a4073 100644 --- a/spec/govuk_error/configuration_spec.rb +++ b/spec/govuk_error/configuration_spec.rb @@ -121,8 +121,9 @@ expect(GovukStatsd).to receive(:increment).exactly(1).times.with("error_types.standard_error") expect(GovukStatsd).to receive(:increment).exactly(1).times.with("hello_world") - configuration.before_send = lambda do |_error_or_event, _hint| + configuration.before_send = lambda do |error_or_event, _hint| GovukStatsd.increment("hello_world") + error_or_event end configuration.before_send.call(StandardError.new) @@ -130,21 +131,23 @@ end end - context "when the before_send lambda has been overridden twice, the first does not take effect" do + context "when the before_send lambda has been overridden several times, all take effect" do before { stub_const("GovukStatsd", double(Module)) } it "increments the appropriate counters" do ClimateControl.modify SENTRY_CURRENT_ENV: "production" do configuration.active_sentry_environments << "production" expect(GovukStatsd).to receive(:increment).exactly(1).times.with("errors_occurred") expect(GovukStatsd).to receive(:increment).exactly(1).times.with("error_types.standard_error") - expect(GovukStatsd).not_to receive(:increment).with("does_not_happen") expect(GovukStatsd).to receive(:increment).exactly(1).times.with("hello_world") + expect(GovukStatsd).to receive(:increment).exactly(1).times.with("hello_world_again") - configuration.before_send = lambda do |_error_or_event, _hint| - GovukStatsd.increment("does_not_happen") - end - configuration.before_send = lambda do |_error_or_event, _hint| + configuration.before_send = lambda do |error_or_event, _hint| GovukStatsd.increment("hello_world") + error_or_event + end + configuration.before_send = lambda do |error_or_event, _hint| + GovukStatsd.increment("hello_world_again") + error_or_event end configuration.before_send.call(StandardError.new) @@ -159,11 +162,26 @@ raven_configurator = GovukError::Configuration.new(Raven.configuration) raven_configurator.active_sentry_environments << "production" raven_configurator.before_send = lambda do |error_or_event, _hint| - error_or_event == "do capture" + error_or_event if error_or_event == "do capture" end expect(raven_configurator.before_send.call("do capture")).to be_truthy - expect(raven_configurator.before_send.call("don't capture", {})).to be false + expect(raven_configurator.before_send.call("don't capture", {})).to be_nil + end + end + + it "does not increment the counters if the callback returns nil" do + ClimateControl.modify SENTRY_CURRENT_ENV: "production" do + raven_configurator = GovukError::Configuration.new(Raven.configuration) + raven_configurator.active_sentry_environments << "production" + raven_configurator.before_send = lambda do |_error_or_event, _hint| + nil + end + + expect(GovukStatsd).not_to receive(:increment).with("errors_occurred") + expect(GovukStatsd).not_to receive(:increment).with("error_types.standard_error") + + raven_configurator.before_send.call(StandardError.new) end end end