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

[rails] support string errors in error reporter #2464

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add `include_sentry_event` matcher for RSpec [#2424](https://github.com/getsentry/sentry-ruby/pull/2424)
- Add support for Sentry Cache instrumentation, when using Rails.cache ([#2380](https://github.com/getsentry/sentry-ruby/pull/2380))
- Add support for Queue Instrumentation for Sidekiq. [#2403](https://github.com/getsentry/sentry-ruby/pull/2403)
- Add support for string errors in error reporter ([#2464](https://github.com/getsentry/sentry-ruby/pull/2464))
- Reset trace_id and add root transaction for sidekiq-cron [#2446](https://github.com/getsentry/sentry-ruby/pull/2446)

Note: MemoryStore and FileStore require Rails 8.0+
Expand Down
11 changes: 10 additions & 1 deletion sentry-rails/lib/sentry/rails/error_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@
hint.merge!(context.delete(:hint))
end

Sentry::Rails.capture_exception(error, level: severity, contexts: { "rails.error" => context }, tags: tags, hint: hint)
options = { level: severity, contexts: { "rails.error" => context }, tags: tags, hint: hint }

Check warning on line 30 in sentry-rails/lib/sentry/rails/error_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/error_subscriber.rb#L30

Added line #L30 was not covered by tests

case error

Check warning on line 32 in sentry-rails/lib/sentry/rails/error_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/error_subscriber.rb#L32

Added line #L32 was not covered by tests
when String
Sentry::Rails.capture_message(error, **options)

Check warning on line 34 in sentry-rails/lib/sentry/rails/error_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/error_subscriber.rb#L34

Added line #L34 was not covered by tests
when Exception
Sentry::Rails.capture_exception(error, **options)

Check warning on line 36 in sentry-rails/lib/sentry/rails/error_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/error_subscriber.rb#L36

Added line #L36 was not covered by tests
else
raise ArgumentError, "Expected an Exception or a String, got: #{error.inspect}"

Check warning on line 38 in sentry-rails/lib/sentry/rails/error_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/error_subscriber.rb#L38

Added line #L38 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not raise inside the SDK, let's use log_debug here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sl0thentr0py hmm, what's the best way of accessing logger though? I can see that the helper is defined in the LoggingHelper, but not sure which object I should use for logging, or if I should include that helper in the ErrorSubscriber?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you can just include it

end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions sentry-rails/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ def capture_in_separate_process(exit_code:)

expect(transport.events.count).to eq(0)
end

it "captures string messages through error reporter" do
Rails.error.report("Test message", severity: :info, context: { foo: "bar" })

expect(transport.events.count).to eq(1)
event = transport.events.first

expect(event.message).to eq("Test message")
expect(event.level).to eq(:info)
expect(event.contexts).to include({ "rails.error" => { foo: "bar" } })
expect(event.tags).to include({ handled: true })
end

it "crashes if the message is not a string or an exception" do
# Subscriber crashes are handled and logged as fatal, so this doesn't raise here
Rails.error.report(312, severity: :info, context: { foo: "bar" })

expect(transport.events.count).to eq(0)
end
end
end
end
Loading