Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also ignore subclasses of data_sync_excluded_exceptions #165

Merged
merged 4 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/govuk_app_config/govuk_error/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def should_capture=(closure)
def ignore_excluded_exceptions_in_data_sync
lambda { |error_or_event|
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 = Raven::Utils::ExceptionCauseChain.exception_to_array(error_or_event)
exception_chain.any? { |exception| exception.class.to_s == exception_to_ignore }
exception_chain.any? { |exception| exception.is_a?(exception_to_ignore) }
kevindew marked this conversation as resolved.
Show resolved Hide resolved
rescue NameError
# the exception type represented by the exception_to_ignore string
# doesn't even exist in this environment, so won't be found in the chain
false
end

!(data_sync.in_progress? && data_sync_ignored_error)
Expand Down
77 changes: 58 additions & 19 deletions spec/govuk_error/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,75 @@
end

describe ".should_capture" do
around do |example|
ClimateControl.modify GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
example.run
end
end

let(:configuration) { GovukError::Configuration.new(Raven.configuration) }

it "should capture errors that occur during the data sync" do
travel_to(Time.current.change(hour: 23)) do
context "during the data sync" do
around do |example|
ClimateControl.modify GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
travel_to(Time.current.change(hour: 23)) do
example.run
end
end
end

it "should capture errors by default" do
expect(configuration.should_capture.call(StandardError.new)).to eq(true)
end
end

it "should ignore errors that have been added to data_sync_excluded_exceptions, if they occurred during the data sync" do
configuration.data_sync_excluded_exceptions << "StandardError"
it "should ignore errors that have been added as a string to data_sync_excluded_exceptions" do
configuration.data_sync_excluded_exceptions << "StandardError"

expect(configuration.should_capture.call(StandardError.new)).to eq(false)
end

it "should ignore errors that have been added as a class to data_sync_excluded_exceptions" do
configuration.data_sync_excluded_exceptions << StandardError

travel_to(Time.current.change(hour: 23)) do
expect(configuration.should_capture.call(StandardError.new)).to eq(false)
end

it "should ignore errors whose underlying cause is an exception in data_sync_excluded_exceptions" do
stub_const("ErrorWeCareAbout", Class.new(StandardError))
stub_const("SomeOtherError", Class.new(StandardError))
configuration.data_sync_excluded_exceptions << "ErrorWeCareAbout"

chained_exception = nil
begin
begin
raise ErrorWeCareAbout
rescue ErrorWeCareAbout
raise SomeOtherError
end
rescue SomeOtherError => e
chained_exception = e
end

expect(chained_exception).to be_an_instance_of(SomeOtherError)
expect(configuration.should_capture.call(chained_exception)).to eq(false)
end

it "should ignore errors that are subclasses of an exception in data_sync_excluded_exceptions" do
stub_const("SomeClass", Class.new(StandardError))
stub_const("SomeInheritedClass", Class.new(SomeClass))

configuration.data_sync_excluded_exceptions << "SomeClass"
expect(configuration.should_capture.call(SomeInheritedClass.new)).to eq(false)
end
end

it "should ignore exceptions whose underlying cause is an ignorable error, if it occurred during the data sync" do
pg_error = double("Caused by PG::Error", class: "PG::Error")
allow(pg_error).to receive(:cause)
exception = double("Exception 1", cause: double("Exception 2", cause: pg_error))
context "outside of the data sync" do
around do |example|
ClimateControl.modify GOVUK_DATA_SYNC_PERIOD: "22:00-08:00" do
travel_to(Time.current.change(hour: 21)) do
example.run
end
end
end

configuration.data_sync_excluded_exceptions << "PG::Error"
travel_to(Time.current.change(hour: 23)) do
expect(configuration.should_capture.call(exception)).to eq(false)
it "should capture errors even if they are in the list of data_sync_excluded_exceptions" do
configuration.data_sync_excluded_exceptions << "StandardError"

expect(configuration.should_capture.call(StandardError.new)).to eq(true)
end
end
end
Expand Down