Skip to content

Commit

Permalink
Use shorthand lambda syntax
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robinjam committed Jan 25, 2024
1 parent 6400a3b commit 0727d83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
12 changes: 5 additions & 7 deletions lib/govuk_app_config/govuk_error/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand All @@ -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}")
Expand All @@ -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
20 changes: 10 additions & 10 deletions spec/govuk_error/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit 0727d83

Please sign in to comment.