From 1b3c6f4ade73e9419d39847cbae579ae2f75d5fb Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 25 Aug 2021 14:30:12 +0100 Subject: [PATCH] Use the configuration context even if it's 'nil' This allows users to opt out of automatic context setting without having to set their own context for all events --- lib/bugsnag/configuration.rb | 12 ++++++++++++ lib/bugsnag/report.rb | 6 ++++-- spec/report_spec.rb | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) 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"