From 20abb8465744490629b3f0a1856c5c9451db14b5 Mon Sep 17 00:00:00 2001 From: Walt Jones Date: Thu, 5 Dec 2019 16:03:07 -0800 Subject: [PATCH] feat: add rollbar_context to exceptions --- lib/rollbar/configuration.rb | 2 ++ lib/rollbar/item.rb | 10 ++++++++-- lib/rollbar/plugins/error_context.rb | 11 +++++++++++ spec/rollbar/item_spec.rb | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 lib/rollbar/plugins/error_context.rb diff --git a/lib/rollbar/configuration.rb b/lib/rollbar/configuration.rb index edebc25c..57848004 100644 --- a/lib/rollbar/configuration.rb +++ b/lib/rollbar/configuration.rb @@ -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 @@ -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 diff --git a/lib/rollbar/item.rb b/lib/rollbar/item.rb index 042caf31..2a5ed365 100644 --- a/lib/rollbar/item.rb +++ b/lib/rollbar/item.rb @@ -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 diff --git a/lib/rollbar/plugins/error_context.rb b/lib/rollbar/plugins/error_context.rb new file mode 100644 index 00000000..e6354d8a --- /dev/null +++ b/lib/rollbar/plugins/error_context.rb @@ -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 diff --git a/spec/rollbar/item_spec.rb b/spec/rollbar/item_spec.rb index aabcccf8..9001ce9f 100644 --- a/spec/rollbar/item_spec.rb +++ b/spec/rollbar/item_spec.rb @@ -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