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

[bug] [jruby] Java ClassCastException exposed; not wrapped in Ruby Exception #2211

Closed
byteit101 opened this issue Mar 27, 2021 · 1 comment · Fixed by #2636
Closed

[bug] [jruby] Java ClassCastException exposed; not wrapped in Ruby Exception #2211

byteit101 opened this issue Mar 27, 2021 · 1 comment · Fixed by #2636

Comments

@byteit101
Copy link

Actual Behavior

jruby-9.2.7.0 :001 > require 'nokogiri'
 => true 
jruby-9.2.7.0 :002 > Nokogiri::HTML.parse("<p>").css("p")["class"]
Traceback (most recent call last):
            <...snip>
        3: from nokogiri.XmlNodeSet$INVOKER$i$slice.call(XmlNodeSet$INVOKER$i$slice.gen)
        2: from nokogiri.XmlNodeSet.slice(XmlNodeSet.java:356)
        1: from nokogiri.XmlNodeSet.rangeBeginLength(XmlNodeSet.java:314)
Java::JavaLang::ClassCastException (org.jruby.RubyString cannot be cast to org.jruby.RubyRange)
jruby-9.2.7.0 :003 > 

I'm aware the correct syntax is Nokogiri::HTML.parse("<p>").css("p").first["class"]. This bug report is about an error in the error :-)

Expected behavior

If there is an exception, it is a Ruby Exception, not a Java Exception, as shown on MRI:

2.6.2 :001 > require 'nokogiri'
 => true 
2.6.2 :002 > Nokogiri::HTML.parse("<p>").css("p")["class"]
Traceback (most recent call last):
            <...snip>
        2: from (irb):2
        1: from (irb):2:in `[]'
TypeError (no implicit conversion of String into Integer)
2.6.2 :003 > 

Environment

# Nokogiri (1.11.2)
    ---
    warnings: []
    nokogiri:
      version: 1.11.2
    ruby:
      version: 2.5.3
      platform: java
      gem_platform: universal-java-1.8
      description: jruby 9.2.7.0 (2.5.3) 2019-04-09 8a269e3 Java HotSpot(TM) 64-Bit Server
        VM 25.201-b09 on 1.8.0_201-b09 +jit [linux-x86_64]
      engine: jruby
      jruby: 9.2.7.0
    other_libraries:
      xerces: Xerces-J 2.12.0
      nekohtml: NekoHTML 1.9.21
@byteit101 byteit101 added the state/needs-triage Inbox for non-installation-related bug reports or help requests label Mar 27, 2021
@flavorjones
Copy link
Member

flavorjones commented Mar 29, 2021

@byteit101 Thanks for opening this issue. Likely I'll wait until v1.12 to address this, but we always try to have the same exceptions raised in both implementations.

Side note: You may also want to use Node#at_css here which is the same as css&.first, if you're going to throw away results. At some point in the future we may optimize this call to be more efficient in the underlying XPath query.

@flavorjones flavorjones added platform/jruby and removed state/needs-triage Inbox for non-installation-related bug reports or help requests labels Mar 29, 2021
@flavorjones flavorjones added this to the v1.14.0 milestone Jan 26, 2022
flavorjones added a commit that referenced this issue Aug 26, 2022
Previously this was raising a Java exception

Closes #2211
flavorjones added a commit that referenced this issue Aug 27, 2022
Previously this was raising a Java exception

Closes #2211
flavorjones added a commit that referenced this issue Aug 27, 2022
Previously this was raising a Java exception

Closes #2211
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants