Skip to content

JRuby: unable to capture Java exceptions #2043

@richard-kramer

Description

@richard-kramer

Issue Description

Using sentry in JRuby, it is not possible to directly capture exceptions, which originated in java, as they do not inherit from Exception.

Currently, the only ways to log these exceptions, would be to wrap the Java exception in a custom Ruby exception an pass that to Sentry or to patch the is_a? method on the Java exception to respond with true if Exception is passed. Both solutions don't really seem that clean.

I also submitted an issue over at JRuby, but it seems that behavior is intended. Java exceptions inherit from Java::JavaLang::Error and Java::JavaLang::Throwable, so maybe one of those (probably Java::JavaLang::Throwable) could be added to the type check in JRuby?

Reproduction Steps

The following snippet enters the rescue block, but the exception cannot be captured:

begin
  raise java.lang.OutOfMemoryError.new("A Java error")
rescue Exception => e
  e.is_a?(Exception) # => false
  Sentry.capture_exception(e) # => expect the argument to be a Exception, got Java::JavaLang::OutOfMemoryError (java.lang.OutOfMemoryError: A Java error)
end

Expected Behavior

The exception can be captured by Sentry as it should respond to the expected interface of an exception.

Actual Behavior

Capturing the exception results in an error thrown by sentry because of this type check.

Ruby Version

jruby-9.2.20.0

SDK Version

5.5.0

Integration and Its Version

sentry-rails (5.5.0)

Sentry Config

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions