Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge hash instead of replacing the original value in user_context #1064

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/raven/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ def annotate_exception(exc, options = {})
# Raven.user_context('id' => 1, 'email' => 'foo@example.com')
def user_context(options = nil)
original_user_context = context.user
context.user = options || {}

if options
context.user.merge!(options)
else
context.user = {}
end

yield if block_given?
context.user
ensure
Expand Down
10 changes: 8 additions & 2 deletions spec/raven/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ def ivars(object)

describe "#user_context" do
context "without a block" do
it "doesn't override previous data" do
subject.user_context(id: 1)
subject.user_context(email: "test@example.com")

expect(subject.context.user).to eq({ id: 1, email: "test@example.com" })
end
it "empties the user context when called without options" do
subject.context.user = { id: 1 }
expect(subject.user_context).to eq({})
Expand All @@ -257,9 +263,9 @@ def ivars(object)
expect(subject.user_context(nil)).to eq({})
end

it "empties the user context when called with {}" do
it "doesn't empty the user context when called with {}" do
subject.context.user = { id: 1 }
expect(subject.user_context({})).to eq({})
expect(subject.user_context({})).to eq({ id: 1 })
end

it "returns the user context when set" do
Expand Down