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

Add hint: support to Sentry::Rails::ErrorSubscriber #2235

Merged
merged 2 commits into from
Jan 17, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Unreleased

### Features

- Fix warning about default gems on Ruby 3.3.0 ([#2225](https://github.com/getsentry/sentry-ruby/pull/2225))
- Add `hint:` support to `Sentry::Rails::ErrorSubscriber` [#2235](https://github.com/getsentry/sentry-ruby/pull/2235)

## 5.16.1

Expand Down
8 changes: 7 additions & 1 deletion sentry-rails/lib/sentry/rails/error_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def report(error, handled:, severity:, context:, source: nil)
tags.merge!(context.delete(:tags))
end

Sentry::Rails.capture_exception(error, level: severity, contexts: { "rails.error" => context }, tags: tags)
hint = {}
if context[:hint].is_a?(Hash)
context = context.dup
hint.merge!(context.delete(:hint))
end

Sentry::Rails.capture_exception(error, level: severity, contexts: { "rails.error" => context }, tags: tags, hint: hint)
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions sentry-rails/spec/sentry/rails/error_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,41 @@
end
end
end

context 'when passed a context with hint key' do
context 'when hint is a Hash' do
it 'merges the hint into the event' do
expect(Sentry::Rails).to receive(:capture_exception) do |_, hint:, **_|
expect(hint[:foo]).to eq('bar')
end
described_class.new.report(StandardError.new, handled: true, severity: :error, context: { hint: { foo: 'bar' } })
end

it 'does not pass the hint to the context' do
expect(Sentry::Rails).to receive(:capture_exception) do |_, contexts:, **_|
expect(contexts["rails.error"]).not_to have_key(:hint)
end
described_class.new.report(StandardError.new, handled: true, severity: :error, context: { hint: { foo: 'bar' } })
end
end


context 'when hint is not a Hash' do
it 'does not merge the hint into the event' do
expect(Sentry::Rails).to receive(:capture_exception) do |_, hint:, **_|
expect(hint).not_to have_key(:foo)
end
described_class.new.report(StandardError.new, handled: true, severity: :error, context: { hint: 'foo' })
end

it 'passes the hint to the context' do
expect(Sentry::Rails).to receive(:capture_exception) do |_, contexts:, **_|
expect(contexts["rails.error"][:hint]).to eq('foo')
end
described_class.new.report(StandardError.new, handled: true, severity: :error, context: { hint: 'foo' })
end
end
end

end
end
Loading