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

Add rollbar_context to exceptions #919

Merged
merged 1 commit into from
Dec 10, 2019
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
2 changes: 2 additions & 0 deletions lib/rollbar/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Configuration
attr_accessor :disable_monkey_patch
attr_accessor :disable_rack_monkey_patch
attr_accessor :disable_core_monkey_patch
attr_accessor :enable_error_context
attr_accessor :dj_threshold
attr_accessor :enabled
attr_accessor :endpoint
Expand Down Expand Up @@ -88,6 +89,7 @@ def initialize
@disable_monkey_patch = false
@disable_core_monkey_patch = false
@disable_rack_monkey_patch = false
@enable_error_context = true
@dj_threshold = 0
@enabled = nil # set to true when configure is called
@endpoint = DEFAULT_ENDPOINT
Expand Down
10 changes: 8 additions & 2 deletions lib/rollbar/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,19 @@ def build_backtrace_body
end

def build_extra
merged_extra = Util.deep_merge(scrub(extra), scrub(error_context))

if custom_data_method? && !Rollbar::Util.method_in_stack(:custom_data, __FILE__)
Util.deep_merge(scrub(custom_data), scrub(extra) || {})
Util.deep_merge(scrub(custom_data), merged_extra)
else
scrub(extra)
merged_extra.empty? ? nil : merged_extra # avoid putting an empty {} in the payload.
end
end

def error_context
exception.respond_to?(:rollbar_context) && exception.rollbar_context
end

def scrub(data)
return data unless data.is_a? Hash

Expand Down
11 changes: 11 additions & 0 deletions lib/rollbar/plugins/error_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module RollbarErrorContext
attr_accessor :rollbar_context
end

Rollbar.plugins.define('error_context') do
dependency { configuration.enable_error_context }

execute! do
StandardError.send(:include, RollbarErrorContext)
end
end
16 changes: 16 additions & 0 deletions spec/rollbar/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@
end
end

context 'with error context' do
let(:context) do
{:key => 'value', :hash => {:inner_key => 'inner_value'}}
end
it 'should build exception data with a extra data' do
exception.rollbar_context = context

body = payload['data'][:body]
trace = body[:trace]

trace[:exception][:message].should match(/^(undefined local variable or method `bar'|undefined method `bar' on an instance of)/)
trace[:extra][:key].should == 'value'
trace[:extra][:hash].should == {:inner_key => 'inner_value'}
end
end

context 'with nested exceptions' do
let(:crashing_code) do
proc do
Expand Down