Skip to content

Commit

Permalink
apply capture_uncaught config to rails error subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Aug 27, 2024
1 parent 2d71966 commit b56976b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/rollbar/plugins/rails/error_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Rollbar
class ErrorSubscriber
def report(error, handled:, severity:, context:, source: nil)
# The default `nil` for capture_uncaught means `true`. so check for false.
return unless handled || Rollbar.configuration.capture_uncaught != false

extra = context.is_a?(Hash) ? context.deep_dup : {}
extra[:custom_data_method_context] = source
Rollbar.log(severity, error, extra)
Expand Down
31 changes: 31 additions & 0 deletions spec/controllers/home_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ def send_req(meth, path, args)
before do
Rollbar.configure do |config|
config.enable_rails_error_subscriber = true
config.capture_uncaught = nil
end
end

after do
Rollbar.configure do |config|
config.enable_rails_error_subscriber = false
config.capture_uncaught = nil
end
end

Expand All @@ -324,6 +326,21 @@ def send_req(meth, path, args)
get '/handle_rails_error'
end

it '`handle` should report a warning via rails error subscriber when capture_uncaught is false' do
logger_mock.should_receive(:info).with('[Rollbar] Success').never

Rollbar.configure do |config|
config.capture_uncaught = false
end

expect(Rollbar).to receive(:log) do |level, _, extra|
expect(extra[:custom_data_method_context]).to be_eql('application')
expect(level.to_s).to be_eql('warning')
end

get '/handle_rails_error'
end

it '`report` should raise an error and report an error via rails error subscriber' do
logger_mock.should_receive(:info).with('[Rollbar] Success').never

Expand All @@ -349,6 +366,20 @@ def send_req(meth, path, args)
get '/cause_exception'
end.to raise_exception(NameError, 'Uncaught Rails error')
end

it 'uncaught exception should not report an error when capture_uncaught is not set' do
logger_mock.should_receive(:info).with('[Rollbar] Success').never

Rollbar.configure do |config|
config.capture_uncaught = false
end

expect(Rollbar).to receive(:log).never

expect do
get '/cause_exception'
end.to raise_exception(NameError, 'Uncaught Rails error')
end
end

context 'when Rails Error Subscriber is enabled in unsupported Rails', if: ::Rails.gem_version < ::Gem::Version.new('7.1.0') do
Expand Down

0 comments on commit b56976b

Please sign in to comment.