diff --git a/lib/bugsnag/configuration.rb b/lib/bugsnag/configuration.rb index 96da7fd6..f1cba934 100644 --- a/lib/bugsnag/configuration.rb +++ b/lib/bugsnag/configuration.rb @@ -553,6 +553,18 @@ def remove_on_breadcrumb(callback) @on_breadcrumb_callbacks.remove(callback) end + ## + # Has the context been explicitly set? + # + # This is necessary to differentiate between the context not being set and + # the context being set to 'nil' explicitly + # + # @api private + # @return [Boolean] + def context_set? + defined?(@context) != nil + end + # TODO: These methods can be a simple attr_accessor when they replace the # methods they are aliasing # NOTE: they are not aliases as YARD doesn't allow documenting the non-alias diff --git a/lib/bugsnag/report.rb b/lib/bugsnag/report.rb index 53cf52f0..0aa59952 100644 --- a/lib/bugsnag/report.rb +++ b/lib/bugsnag/report.rb @@ -114,7 +114,7 @@ def initialize(exception, passed_configuration, auto_notify=false) self.app_type = configuration.app_type self.app_version = configuration.app_version self.breadcrumbs = [] - self.context = configuration.context + self.context = configuration.context if configuration.context_set? self.delivery_method = configuration.delivery_method self.hostname = configuration.hostname self.runtime_versions = configuration.runtime_versions.dup @@ -130,7 +130,9 @@ def initialize(exception, passed_configuration, auto_notify=false) # @!attribute context # @return [String, nil] def context - @context || @automatic_context + return @context if defined?(@context) + + @automatic_context end attr_writer :context diff --git a/spec/report_spec.rb b/spec/report_spec.rb index 58a523a8..14f36dcc 100644 --- a/spec/report_spec.rb +++ b/spec/report_spec.rb @@ -610,6 +610,21 @@ def gloops }) end + it "uses overridden context even it is set to 'nil'" do + Bugsnag.configure do |config| + config.context = nil + end + + Bugsnag.notify(BugsnagTestException.new("It crashed")) do |report| + report.automatic_context = "automatic context" + end + + expect(Bugsnag).to(have_sent_notification { |payload, _headers| + event = get_event_from_payload(payload) + expect(event["context"]).to be_nil + }) + end + it "uses the context from Configuration, if set" do Bugsnag.configure do |config| config.context = "example context"