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

Allow java exceptions as capture type in JRuby (#2043) #2044

Merged
merged 1 commit into from
Jun 7, 2023
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
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