Skip to content

Validate contexts when setting multiple contexts #2022

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

Merged
merged 4 commits into from
Mar 20, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
- `config.enable_tracing = false` will turn off tracing even if `traces_sample_rate/traces_sampler` is set
- `config.enable_tracing = nil` (default) will keep the current behaviour

### Bug Fixes

- Validate that contexts set in `set_contexts` are also Hash instances [#2022](https://github.com/getsentry/sentry-ruby/pull/2022/files)
- Fixes [#2021](https://github.com/getsentry/sentry-ruby/issues/2021)

## 5.8.0

### Features
Expand Down
4 changes: 4 additions & 0 deletions sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def set_tag(key, value)
# @return [Hash]
def set_contexts(contexts_hash)
check_argument_type!(contexts_hash, Hash)
contexts_hash.values.each do |val|
check_argument_type!(val, Hash)
end

@contexts.merge!(contexts_hash) do |key, old, new|
old.merge(new)
end
Expand Down
6 changes: 6 additions & 0 deletions sentry-ruby/spec/sentry/scope/setters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
end.to raise_error(ArgumentError)
end

it "raises error when passed non-hash context value" do
expect do
subject.set_contexts({ character: "John" })
end.to raise_error(ArgumentError)
end

it "merges the context hash" do
subject.set_contexts({ character: { name: "John" }})
expect(subject.contexts).to include({ character: { name: "John" }})
Expand Down