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

Hash#fetch with incorrect key throws different error messages but MRI #1633

Closed
deepj opened this issue Mar 22, 2019 · 2 comments

Comments

3 participants
@deepj
Copy link

commented Mar 22, 2019

To reproduce:

{}.fetch(:object_name)

This throws KeyError (key object_name not found) in TruffleRuby 1.0.0 RC14. In MRI throws KeyError (key not found: :object_name) in this case. Just note: when string is used in fetch, the error message is KeyError (key not found: "object_name").

Yes, this causes problems in some specs/tests such as in FactoryBot/hanami/dry-rb.

Example from FactoryBot specs

1) FactoryBot::Registry raises when an object cannot be found
     Failure/Error: expect { subject.find(:object_name) }.

       expected KeyError with "Great thing not registered: \"object_name\"", got #<KeyError: key object_name not found> with backtrace:
         # ~/.gem/truffleruby/2.6.2/gems/activesupport-5.2.2.1/lib/active_support/hash_with_indifferent_access.rb:177:in `fetch'
         # ~/.gem/truffleruby/2.6.2/gems/activesupport-5.2.2.1/lib/active_support/hash_with_indifferent_access.rb:177:in `fetch'
         # ./lib/factory_bot/registry.rb:23:in `find'
         # ./spec/factory_bot/registry_spec.rb:20:in `block (3 levels) in <top (required)>'
         # ./spec/factory_bot/registry_spec.rb:20:in `block (2 levels) in <top (required)>'
         # ./spec/spec_helper.rb:32:in `block (2 levels) in <top (required)>'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:74:in `load'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:74:in `load'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:74:in `kernel_load'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:28:in `run'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli.rb:463:in `exec'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli.rb:27:in `dispatch'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli.rb:18:in `start'
         # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/friendly_errors.rb:124:in `with_friendly_errors'
     # ./spec/factory_bot/registry_spec.rb:20:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:32:in `block (2 levels) in <top (required)>'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:74:in `load'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:74:in `load'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:74:in `kernel_load'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli/exec.rb:28:in `run'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli.rb:463:in `exec'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli.rb:27:in `dispatch'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/cli.rb:18:in `start'
     # ~/.rubies/truffleruby-1.0.0-rc14/lib/mri/bundler/friendly_errors.rb:124:in `with_friendly_errors'

TruffleRuby would follow MRI in this case.

@deepj deepj changed the title Hash#fetch with incorrect key throw different error messages but MRI Hash#fetch with incorrect key throws different error messages but MRI Mar 24, 2019

@nirvdrum nirvdrum self-assigned this Apr 2, 2019

@nirvdrum

This comment has been minimized.

Copy link
Member

commented Apr 2, 2019

Thanks for the report. I'll add a new spec and make our message match MRI's.

I know many libraries are still targeting versions older than Ruby 2.5. For projects that can do feature detection or inspect the Ruby version, Ruby 2.5 enhanced KeyError with key and receiver methods, which hopefully obviate the need for inspecting the error message programmatically.

@nirvdrum

This comment has been minimized.

Copy link
Member

commented Apr 8, 2019

This is all fixed as of c90cf8e. I updated the message in ENV#fetch to match MRI's exception message format as well. This should be part of the next release (1.0.0-rc16). Thanks again for the detailed report!

@nirvdrum nirvdrum closed this Apr 8, 2019

@nirvdrum nirvdrum added this to the 1.0.0-rc16 milestone Apr 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.