Skip to content

Commit

Permalink
Use the configuration context even if it's 'nil'
Browse files Browse the repository at this point in the history
This allows users to opt out of automatic context setting without
having to set their own context for all events
  • Loading branch information
imjoehaines committed Aug 25, 2021
1 parent 8f0f031 commit 1b3c6f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/bugsnag/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1b3c6f4

Please sign in to comment.