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

feat: return block value if block was passed to Honeybadger.context #546

Merged
merged 1 commit into from
May 10, 2024
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
6 changes: 4 additions & 2 deletions lib/honeybadger/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ def track_deployment(environment: nil, revision: nil, local_username: nil, repos
# When present, tags will be applied to errors with this context
# (optional).
#
# @return [self] so that method calls can be chained.
# @return [Object, self] value of the block if passed, otherwise self
def context(context = nil, &block)
context_manager.set_context(context, &block) unless context.nil?
block_result = context_manager.set_context(context, &block) unless context.nil?
return block_result if block_given?

self
end

Expand Down
20 changes: 20 additions & 0 deletions spec/unit/honeybadger/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@
end
end

describe '#context' do
let(:config) { Honeybadger::Config.new(api_key:'fake api key', logger: NULL_LOGGER) }
subject(:instance) { described_class.new(config) }

it 'sets the context' do
instance.context({a: "context"})
expect(instance.get_context).to eq({a: "context"})
end

it 'allows chaining' do
expect(instance.context({a: "context"})).to eq(instance)
end

context 'with local context' do
it 'returns the return value of the block' do
expect(instance.context({ bar: :baz }) { :return_value }).to eq(:return_value)
end
end
end

describe "#clear!" do
it 'clears all transactional data' do
config = Honeybadger::Config.new(api_key:'fake api key', logger: NULL_LOGGER)
Expand Down