Skip to content

Commit

Permalink
feat: Allow block on tags_context and extra_context (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
rivayama authored and HazAT committed Oct 16, 2019
1 parent 2383bba commit 0b25744
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/raven/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def user_context(options = nil)
# Raven.tags_context('my_custom_tag' => 'tag_value')
def tags_context(options = nil)
context.tags.merge!(options || {})
yield if block_given?

This comment has been minimized.

Copy link
@tycooon

tycooon Oct 16, 2019

This change has broken the return value of the method which used to be context.tags. Some people were relying on that :(

ensure
context.tags.delete_if { |k, _| options.keys.include? k } if block_given?
end

# Bind extra context. Merges with existing context (if any).
Expand All @@ -194,6 +197,9 @@ def tags_context(options = nil)
# Raven.extra_context('my_custom_data' => 'value')
def extra_context(options = nil)
context.extra.merge!(options || {})
yield if block_given?
ensure
context.extra.delete_if { |k, _| options.keys.include? k } if block_given?
end

def rack_context(env)
Expand Down
56 changes: 56 additions & 0 deletions spec/raven/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,62 @@ def ivars(object)
end
end

describe "#tags_context" do
let(:default) { { :foo => :bar } }
let(:additional) { { :baz => :qux } }

before do
subject.context.tags = default
end

it "doesn't set anything if the tags is empty" do
subject.tags_context({})
expect(subject.context.tags).to eq default
end

it "adds tags" do
subject.tags_context(additional)
expect(subject.context.tags).to eq default.merge(additional)
end

context 'when block given' do
it "adds tags only in the block" do
subject.tags_context(additional) do
expect(subject.context.tags).to eq default.merge(additional)
end
expect(subject.context.tags).to eq default
end
end
end

describe "#extra_context" do
let(:default) { { :foo => :bar } }
let(:additional) { { :baz => :qux } }

before do
subject.context.extra = default
end

it "doesn't set anything if the extra is empty" do
subject.extra_context({})
expect(subject.context.extra).to eq default
end

it "adds extra" do
subject.extra_context(additional)
expect(subject.context.extra).to eq default.merge(additional)
end

context 'when block given' do
it "adds extra only in the block" do
subject.extra_context(additional) do
expect(subject.context.extra).to eq default.merge(additional)
end
expect(subject.context.extra).to eq default
end
end
end

describe "#rack_context" do
it "doesn't set anything if the context is empty" do
subject.rack_context({})
Expand Down

0 comments on commit 0b25744

Please sign in to comment.