diff --git a/lib/rollbar/configuration.rb b/lib/rollbar/configuration.rb index 5d0d79af..577cb701 100644 --- a/lib/rollbar/configuration.rb +++ b/lib/rollbar/configuration.rb @@ -26,6 +26,7 @@ class Configuration :enabled, :endpoint, :environment, + :exception_level_filter, :exception_level_filters, :failover_handlers, :filepath, @@ -106,6 +107,7 @@ def initialize @enabled = nil # set to true when configure is called @endpoint = DEFAULT_ENDPOINT @environment = nil + @exception_level_filter = nil @exception_level_filters = { 'ActiveRecord::RecordNotFound' => 'warning', 'AbstractController::ActionNotFound' => 'warning', diff --git a/lib/rollbar/notifier.rb b/lib/rollbar/notifier.rb index c6253c46..f149fd06 100644 --- a/lib/rollbar/notifier.rb +++ b/lib/rollbar/notifier.rb @@ -488,11 +488,15 @@ def filtered_level(exception) return unless exception filter = configuration.exception_level_filters[exception.class.name] - if filter.respond_to?(:call) + level = if filter.respond_to?(:call) filter.call(exception) else filter end + + return level if level && !level.empty? + + configuration.exception_level_filter&.call(exception) end def report(level, message, exception, extra, context) diff --git a/spec/rollbar_spec.rb b/spec/rollbar_spec.rb index 6f84fe7a..7d0041f4 100644 --- a/spec/rollbar_spec.rb +++ b/spec/rollbar_spec.rb @@ -843,17 +843,101 @@ end context 'without use_exception_level_filters argument' do + context 'with explicit exception_level_filters' do + it 'sends the correct filtered level' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'warning' } + end + + Rollbar.error(exception) + + expect(Rollbar.last_report[:level]).to be_eql('warning') + end + + it 'ignores ignored exception classes' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'ignore' } + end + + logger_mock.should_not_receive(:info) + logger_mock.should_not_receive(:warn) + logger_mock.should_not_receive(:error) + + Rollbar.error(exception) + end + + it 'should not use the filters if overriden at log site' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'ignore' } + end + + Rollbar.error(exception, :use_exception_level_filters => false) + + expect(Rollbar.last_report[:level]).to be_eql('error') + end + end + + context 'with dynamic exception_level_filter' do + it 'sends the correct filtered level' do + Rollbar.configure do |config| + config.exception_level_filter = lambda { |_error| 'warning' } + end + + Rollbar.error(exception) + + expect(Rollbar.last_report[:level]).to be_eql('warning') + end + + it 'ignores ignored exception classes' do + Rollbar.configure do |config| + config.exception_level_filter = lambda { |_error| 'ignore' } + end + + logger_mock.should_not_receive(:info) + logger_mock.should_not_receive(:warn) + logger_mock.should_not_receive(:error) + + Rollbar.error(exception) + end + + it 'should not use the filters if overriden at log site' do + Rollbar.configure do |config| + config.exception_level_filter = ->(exception) { 'ignore' } + end + + Rollbar.error(exception, :use_exception_level_filters => false) + + expect(Rollbar.last_report[:level]).to be_eql('error') + end + end + + context 'with both explicit and dynamic exception_level_filter' do + it 'send the explicit filtered level' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'info' } + config.exception_level_filter = ->(exception) { 'warning' } + end + + Rollbar.error(exception) + + expect(Rollbar.last_report[:level]).to be_eql('info') + end + end + end + end + + context 'using :use_exception_level_filters option as true' do + context 'with explicit exception_level_filters' do it 'sends the correct filtered level' do Rollbar.configure do |config| config.exception_level_filters = { 'NameError' => 'warning' } end - Rollbar.error(exception) - + Rollbar.error(exception, :use_exception_level_filters => true) expect(Rollbar.last_report[:level]).to be_eql('warning') end - it 'ignore ignored exception classes' do + it 'ignores ignored exception classes' do Rollbar.configure do |config| config.exception_level_filters = { 'NameError' => 'ignore' } end @@ -862,120 +946,125 @@ logger_mock.should_not_receive(:warn) logger_mock.should_not_receive(:error) - Rollbar.error(exception) + Rollbar.error(exception, :use_exception_level_filters => true) end - it 'should not use the filters if overriden at log site' do + it 'sets error level using lambda' do Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'ignore' } + config.exception_level_filters = { + 'NameError' => lambda { |_error| 'info' } + } end - Rollbar.error(exception, :use_exception_level_filters => false) - - expect(Rollbar.last_report[:level]).to be_eql('error') - end - end - end + logger_mock.should_receive(:info) + logger_mock.should_not_receive(:warn) + logger_mock.should_not_receive(:error) - context 'using :use_exception_level_filters option as true' do - it 'sends the correct filtered level' do - Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'warning' } + Rollbar.error(exception, :use_exception_level_filters => true) end - Rollbar.error(exception, :use_exception_level_filters => true) - expect(Rollbar.last_report[:level]).to be_eql('warning') - end + context 'using :use_exception_level_filters option as false' do + it 'sends the correct filtered level' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'warning' } + end - it 'ignore ignored exception classes' do - Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'ignore' } - end + Rollbar.error(exception, :use_exception_level_filters => false) + expect(Rollbar.last_report[:level]).to be_eql('error') + end - logger_mock.should_not_receive(:info) - logger_mock.should_not_receive(:warn) - logger_mock.should_not_receive(:error) + it 'ignores ignored exception classes' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'ignore' } + end - Rollbar.error(exception, :use_exception_level_filters => true) - end + Rollbar.error(exception, :use_exception_level_filters => false) - it 'sets error level using lambda' do - Rollbar.configure do |config| - config.exception_level_filters = { - 'NameError' => lambda { |_error| 'info' } - } + expect(Rollbar.last_report[:level]).to be_eql('error') + end end - - logger_mock.should_receive(:info) - logger_mock.should_not_receive(:warn) - logger_mock.should_not_receive(:error) - - Rollbar.error(exception, :use_exception_level_filters => true) end - context 'using :use_exception_level_filters option as false' do + context 'with dynamic exception_level_filter' do it 'sends the correct filtered level' do Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'warning' } + config.exception_level_filter = lambda { |_error| 'warning' } end - Rollbar.error(exception, :use_exception_level_filters => false) - expect(Rollbar.last_report[:level]).to be_eql('error') + Rollbar.error(exception, :use_exception_level_filters => true) + + expect(Rollbar.last_report[:level]).to be_eql('warning') end - it 'ignore ignored exception classes' do + it 'ignores ignored exception classes' do Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'ignore' } + config.exception_level_filter = lambda { |_error| 'ignore' } end - Rollbar.error(exception, :use_exception_level_filters => false) + logger_mock.should_not_receive(:info) + logger_mock.should_not_receive(:warn) + logger_mock.should_not_receive(:error) - expect(Rollbar.last_report[:level]).to be_eql('error') + Rollbar.error(exception, :use_exception_level_filters => true) end - end - end - context 'using :use_exception_level_filters option as true' do - it 'sends the correct filtered level' do - Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'warning' } - end + context 'using :use_exception_level_filters option as false' do + it 'sends the correct filtered level' do + Rollbar.configure do |config| + config.exception_level_filter = lambda { |_error| 'ignore' } + end - Rollbar.error(exception, :use_exception_level_filters => true) - expect(Rollbar.last_report[:level]).to be_eql('warning') - end + Rollbar.error(exception, :use_exception_level_filters => false) + expect(Rollbar.last_report[:level]).to be_eql('error') + end - it 'ignore ignored exception classes' do - Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'ignore' } - end + it 'ignores ignored exception classes' do + Rollbar.configure do |config| + config.exception_level_filter = lambda { |_error| 'ignore' } + end - logger_mock.should_not_receive(:info) - logger_mock.should_not_receive(:warn) - logger_mock.should_not_receive(:error) + Rollbar.error(exception, :use_exception_level_filters => false) - Rollbar.error(exception, :use_exception_level_filters => true) + expect(Rollbar.last_report[:level]).to be_eql('error') + end + end end end context 'if not using :use_exception_level_filters option' do - it 'sends the level defined by the used method' do - Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'warning' } - end + context 'with explicit exception_level_filters' do + it 'sends the level defined by the used method' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'warning' } + end - Rollbar.error(exception) - expect(Rollbar.last_report[:level]).to be_eql('error') + Rollbar.error(exception) + expect(Rollbar.last_report[:level]).to be_eql('error') + end end - it 'ignore ignored exception classes' do - Rollbar.configure do |config| - config.exception_level_filters = { 'NameError' => 'ignore' } + context 'with both explicit and dynamic exception_level_filter' do + it 'send the explicit filtered level' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'info' } + config.exception_level_filter = lambda { |_error| 'warning' } + end + + Rollbar.error(exception, :use_exception_level_filters => true) + + expect(Rollbar.last_report[:level]).to be_eql('info') end - Rollbar.error(exception) + it 'ignores ignored exception classes' do + Rollbar.configure do |config| + config.exception_level_filters = { 'NameError' => 'ignore' } + config.exception_level_filter = lambda { |_error| 'warning' } + end - expect(Rollbar.last_report[:level]).to be_eql('error') + Rollbar.error(exception) + + expect(Rollbar.last_report[:level]).to be_eql('error') + end end end