Skip to content

Commit

Permalink
[rails] support string errors in error reporter (#2464)
Browse files Browse the repository at this point in the history
* [rails] support string errors in error reporter

* Update CHANGELOG

* [rails] log instead of raising when ErrorSubscriber fails

* [rails] fix spec
  • Loading branch information
solnic authored Nov 16, 2024
1 parent 5de4ebc commit b31f0f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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
15 changes: 14 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,20 @@ def report(error, handled:, severity:, context:, source: nil)
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 }

case error
when String
Sentry::Rails.capture_message(error, **options)
when Exception
Sentry::Rails.capture_exception(error, **options)
else
log_debug("Expected an Exception or a String, got: #{error.inspect}")
end
end

def log_debug(message)
Sentry.configuration.logger.debug(message)
end
end
end
Expand Down
26 changes: 26 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,32 @@ 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, handled: true, 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 "skips non-string and non-exception errors" do
expect {
Sentry.init do |config|
config.logger = Logger.new($stdout)
end

Sentry.logger.debug("Expected an Exception or a String, got: #{312.inspect}")

Rails.error.report(312, severity: :info, handled: true, context: { foo: "bar" })
}.to output(/Expected an Exception or a String, got: 312/).to_stdout

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

0 comments on commit b31f0f3

Please sign in to comment.