diff --git a/CHANGELOG.md b/CHANGELOG.md index fd3113807..ffc78c6e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Move `http.query` to span data in net/http integration [#2039](https://github.com/getsentry/sentry-ruby/pull/2039) - Validate `release` is a `String` during configuration [#2040](https://github.com/getsentry/sentry-ruby/pull/2040) +- Allow JRuby Java exceptions to be captured [#2043](https://github.com/getsentry/sentry-ruby/pull/2043) ### Bug Fixes diff --git a/sentry-ruby/lib/sentry/hub.rb b/sentry-ruby/lib/sentry/hub.rb index 92cc2ed6b..2b87b2694 100644 --- a/sentry-ruby/lib/sentry/hub.rb +++ b/sentry-ruby/lib/sentry/hub.rb @@ -116,7 +116,11 @@ def with_child_span(instrumenter: :sentry, **attributes, &block) end def capture_exception(exception, **options, &block) - check_argument_type!(exception, ::Exception) + if RUBY_PLATFORM == "java" + check_argument_type!(exception, ::Exception, ::Java::JavaLang::Throwable) + else + check_argument_type!(exception, ::Exception) + end return if Sentry.exception_captured?(exception) diff --git a/sentry-ruby/spec/sentry/hub_spec.rb b/sentry-ruby/spec/sentry/hub_spec.rb index e5fea23e5..ed001b69a 100644 --- a/sentry-ruby/spec/sentry/hub_spec.rb +++ b/sentry-ruby/spec/sentry/hub_spec.rb @@ -180,10 +180,35 @@ end.to change { transport.events.count }.by(1) end - it "raises error when passing a non-exception object" do - expect do - subject.capture_exception("String") - end.to raise_error(ArgumentError, 'expect the argument to be a Exception, got String ("String")') + if RUBY_PLATFORM == "java" + context 'when called under jRuby' do + let(:exception) do + begin + raise java.lang.OutOfMemoryError, "A Java error" + rescue Exception => exception + exception + end + end + + + it "raises error when passing a non-exception object" do + expect do + subject.capture_exception("String") + end.to raise_error(ArgumentError, 'expect the argument to be a Exception or Java::JavaLang::Throwable, got String ("String")') + end + + it 'allows a java error object to be passed' do + expect do + subject.capture_exception(exception) + end.not_to raise_error + end + end + else + it "raises error when passing a non-exception object" do + expect do + subject.capture_exception("String") + end.to raise_error(ArgumentError, 'expect the argument to be a Exception, got String ("String")') + end end # see https://github.com/getsentry/sentry-ruby/issues/1323