Skip to content

Commit

Permalink
Keep callbacks in an array and run each in turn until one returns nil
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamineskola committed May 12, 2021
1 parent 2bfaa20 commit 7f34614
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
20 changes: 11 additions & 9 deletions lib/govuk_app_config/govuk_error/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions spec/govuk_error/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,33 @@
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)
end
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)
Expand All @@ -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
Expand Down

0 comments on commit 7f34614

Please sign in to comment.