Skip to content

Commit

Permalink
feat: add rollbar_context to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Dec 6, 2019
1 parent 378ce01 commit 20abb84
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
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

0 comments on commit 20abb84

Please sign in to comment.