Skip to content

Commit

Permalink
Allow java exceptions as capture type in JRuby (#2043) (#2044)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-kramer authored Jun 7, 2023
1 parent 91308d3 commit a231150
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
33 changes: 29 additions & 4 deletions sentry-ruby/spec/sentry/hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a231150

Please sign in to comment.