[ruby-core:93293] [Ruby trunk Feature#14278] Ambiguous Exception for OpenSSL::HMAC.digest
From:
merch-redmine@...
Date:
2019-06-20 23:36:52 UTC
List:
ruby-core #93293
Issue #14278 has been updated by jeremyevans0 (Jeremy Evans).
Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN)
ruby -v deleted (ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux-gnu])
Tracker changed from Bug to Feature
I do not think the current error message is a bug, and changing the message would be considered a feature. In general, Ruby TypeError messages are not completely descriptive as to the argument and method names. That is not specific to `OpenSSL::HMAC`. Example:
```ruby
"" + nil
# TypeError (no implicit conversion of nil into String)
{}.merge(nil)
# TypeError (no implicit conversion of nil into Hash)
[] + nil
# TypeError (no implicit conversion of nil into Array)
```
Now, we could change all such error messages, by changing internal APIs to pass the method name, argument name, etc.. However, that is a ton of work for a minor benefit, and it doesn't really fit the dynamic nature of Ruby. The truth is, `nil` is an acceptable value for all of these methods, assuming you define the appropriate methods on it:
```ruby
def nil.to_str; '' end
def nil.to_ary; [] end
def nil.to_hash; {} end
require 'openssl'
OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), nil, 'RubyRuby')
# => "\x06\e\xD0(\x99\xC8\xD9\fQ\xD7\x84\xAAZ\x9D\x9F\xF4\xFF#\x83\x83\x84[\xAD\x87\x7F\xC8\xB6\xF9\xADe\x17!"
"" + nil
# => ""
{}.merge(nil)
# => {}
[] + nil
# => []
```
Once you understand this, you'll probably understand why the error messages are the way they are.
----------------------------------------
Feature #14278: Ambiguous Exception for OpenSSL::HMAC.digest
https://bugs.ruby-lang.org/issues/14278#change-78764
* Author: KINGSABRI (KING SABRI)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
The `OpenSSL::HMAC.digest` shows unclear and ambiguous exception when key is nil.
~~~ ruby
require 'openssl'
key = nil
OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, 'RubyRuby')
TypeError: no implicit conversion of nil into String
`digest'
~~~
**Expected Behavior**
So clear and understandable issue, such:
~~~ text
key argument cannot be nil for OpenSSL::HMAC.digest
~~~
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>