Skip to content

Commit

Permalink
add config option to ignore internal errors (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones authored Oct 27, 2021
1 parent 609fad3 commit 9efecb9
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rollbar/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Configuration
:files_processed_size,
:files_with_pid_name_enabled,
:framework,
:ignore_internal_errors,
:ignored_person_ids,
:js_enabled,
:js_options,
Expand Down Expand Up @@ -112,6 +113,11 @@ def initialize
}
@failover_handlers = []
@framework = 'Plain'
@ignore_internal_errors = [
'Net::ReadTimeout',
'Net::OpenTimeout',
'SocketError'
]
@ignored_person_ids = []
@host = nil
@payload_options = {}
Expand Down
19 changes: 19 additions & 0 deletions lib/rollbar/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ def log_data(data)
# report including the exception traceback. If that fails, we'll fall back
# to a more static failsafe response.
def report_internal_error(exception, original_error = nil)
return if skip_reporting_internal_error(exception)

failsafe_message = ''
log_error(
'[Rollbar] Reporting internal error encountered while sending data to Rollbar.'
Expand All @@ -551,6 +553,23 @@ def report_internal_error(exception, original_error = nil)
log_error(item ? "[Rollbar] Item: #{item}" : "[Rollbar] Exception: #{exception}")
end

def skip_reporting_internal_error(exception)
return true if configuration.ignore_internal_errors == true

configuration.ignore_internal_errors.each do |error_name|
begin
error_cls = error_name.split('::').reduce(Module, :const_get)
return true if exception.class <= error_cls
rescue NameError
# Ignore errors and continue matching.
# It's possible for a class name in the list to not be resolvable,
# and this is ok.
end
end

false
end

## Payload building functions

def build_item(level, message, exception, extra, context)
Expand Down
98 changes: 98 additions & 0 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,104 @@ def backtrace
expect(original_message).to be_eql(message)
end
end

context 'with internal error' do
let(:message) { 'original_error' }
let(:exception) { StandardError.new(message) }

after(:all) do
Rollbar.configure do |config|
config.ignore_internal_errors = [
'Net::ReadTimeout',
'Net::OpenTimeout',
'SocketError'
]
end
end

context 'when internal error is not ignored' do
it 'should report internal error' do
allow(Rollbar.notifier)
.to receive(:build_item)
.and_raise(StandardError.new('internal error'))

expect(Rollbar.notifier).to receive(:process_item).and_call_original
Rollbar.error(exception)

original_error = Rollbar.last_report[:notifier][:diagnostic][:original_error]
expect(original_error[:message]).to be_eql(message)
end
end

context 'when internal error is ignored' do
it 'should not report internal error' do
allow(Rollbar.notifier)
.to receive(:build_item)
.and_raise(Net::ReadTimeout.new('internal error'))

expect(Rollbar.notifier).not_to receive(:process_item)
Rollbar.error(exception)
end
end

context 'when internal error is subclass of ignored error class' do
it 'should not report internal error' do
Rollbar.configure do |config|
config.ignore_internal_errors = ['StandardError']
end

allow(Rollbar.notifier)
.to receive(:build_item)
.and_raise(RuntimeError.new('internal error'))

expect(Rollbar.notifier).not_to receive(:process_item)
Rollbar.error(exception)
end
end

context 'when set to ignore all internal errors' do
it 'should not report internal error' do
Rollbar.configure do |config|
config.ignore_internal_errors = true
end

allow(Rollbar.notifier)
.to receive(:build_item)
.and_raise(RuntimeError.new('internal error'))

expect(Rollbar.notifier).not_to receive(:process_item)
Rollbar.error(exception)
end
end

context 'when ignored error class is unknown' do
it 'should report internal error' do
Rollbar.configure do |config|
config.ignore_internal_errors = ['Net::Foo']
end

allow(Rollbar.notifier)
.to receive(:build_item)
.and_raise(RuntimeError.new('internal error'))

expect(Rollbar.notifier).to receive(:process_item).and_call_original
Rollbar.error(exception)
end

it 'should continue matching the list' do
Rollbar.configure do |config|
config.ignore_internal_errors = ['Net::Foo', 'StandardError']
end

allow(Rollbar.notifier)
.to receive(:build_item)
.and_raise(RuntimeError.new('internal error'))

expect(Rollbar.notifier).not_to receive(:process_item)
Rollbar.error(exception)
end
end
end
end

# Backwards
Expand Down

0 comments on commit 9efecb9

Please sign in to comment.