From 0727d8356a172ba54c24404403c19732458e7e35 Mon Sep 17 00:00:00 2001 From: James Robinson Date: Thu, 25 Jan 2024 10:22:15 +0000 Subject: [PATCH] Use shorthand lambda syntax Fixes #341 `SimpleDelegator` was intercepting the `lambda` call via `method_missing`, and delegating to `Kernel#lambda`, passing the given block. This explodes on Ruby 3.3 because `lambda` only accepts block literals now. Replacing the `lambda` call with this shorthand syntax sidesteps the issue. --- README.md | 4 ++-- .../govuk_error/configuration.rb | 12 +++++------ spec/govuk_error/configuration_spec.rb | 20 +++++++++---------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 6c4618d..f94dc14 100644 --- a/README.md +++ b/README.md @@ -96,9 +96,9 @@ and occurs at the time of a data sync, then it will be excluded even if the cust ```ruby GovukError.configure do |config| - config.before_send = lambda do |event, hint| + config.before_send = -> (event, hint) { hint[:exception].is_a?(ErrorWeWantToIgnore) ? nil : event - end + } end ``` diff --git a/lib/govuk_app_config/govuk_error/configuration.rb b/lib/govuk_app_config/govuk_error/configuration.rb index a90e3fc..061cd86 100644 --- a/lib/govuk_app_config/govuk_error/configuration.rb +++ b/lib/govuk_app_config/govuk_error/configuration.rb @@ -72,9 +72,7 @@ def set_up_defaults ] # Need to invoke an arbitrary `before_send=` in order to trigger the # `before_send_callbacks` behaviour - self.before_send = lambda { |error_or_event, _hint| - error_or_event - } + self.before_send = -> (error_or_event, _hint) { error_or_event } end def before_send=(closure) @@ -85,7 +83,7 @@ def before_send=(closure) protected def ignore_excluded_exceptions_in_data_sync - lambda { |event, hint| + -> (event, hint) { data_sync_ignored_error = data_sync_excluded_exceptions.any? do |exception_to_ignore| exception_to_ignore = Object.const_get(exception_to_ignore) unless exception_to_ignore.is_a?(Module) exception_chain = Sentry::Utils::ExceptionCauseChain.exception_to_array(hint[:exception]) @@ -101,7 +99,7 @@ def ignore_excluded_exceptions_in_data_sync end def increment_govuk_statsd_counters - lambda { |event, hint| + -> (event, hint) { if hint[:exception] GovukStatsd.increment("errors_occurred") GovukStatsd.increment("error_types.#{hint[:exception].class.name.split('::').last.underscore}") @@ -111,14 +109,14 @@ def increment_govuk_statsd_counters end def run_before_send_callbacks - lambda do |event, hint| + -> (event, hint) { result = event @before_send_callbacks.each do |callback| result = callback.call(event, hint) break if result.nil? end result - end + } end end end diff --git a/spec/govuk_error/configuration_spec.rb b/spec/govuk_error/configuration_spec.rb index b0bbf00..672fad0 100644 --- a/spec/govuk_error/configuration_spec.rb +++ b/spec/govuk_error/configuration_spec.rb @@ -22,7 +22,7 @@ describe ".before_send" do let(:configuration) do configuration = GovukError::Configuration.new(Sentry::Configuration.new) - configuration.before_send = lambda { |error_or_event, _hint| + configuration.before_send = -> (error_or_event, _hint) { error_or_event } configuration @@ -132,7 +132,7 @@ 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 = -> (error_or_event, _hint) { GovukStatsd.increment("hello_world") error_or_event end @@ -150,14 +150,14 @@ 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| + configuration.before_send = -> (error_or_event, _hint) { GovukStatsd.increment("hello_world") error_or_event - end - configuration.before_send = lambda do |error_or_event, _hint| + } + configuration.before_send = -> (error_or_event, _hint) { GovukStatsd.increment("hello_world_again") error_or_event - end + } send_exception_to_sentry(StandardError.new, configuration) end @@ -184,9 +184,9 @@ ClimateControl.modify SENTRY_CURRENT_ENV: "production" do sentry_configurator = GovukError::Configuration.new(Sentry::Configuration.new) stub_const("CustomError", Class.new(StandardError)) - sentry_configurator.before_send = lambda do |event, hint| + sentry_configurator.before_send = -> (event, hint) { event if hint[:exception].is_a?(CustomError) - end + } sentry_client = send_exception_to_sentry(CustomError.new, sentry_configurator) expect(sentry_client.transport.events.count).to eq(1) @@ -198,9 +198,9 @@ it "does not increment the counters if the callback returns nil" do ClimateControl.modify SENTRY_CURRENT_ENV: "production" do sentry_configurator = GovukError::Configuration.new(Sentry::Configuration.new) - sentry_configurator.before_send = lambda do |_error_or_event, _hint| + sentry_configurator.before_send = -> (_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")