You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WEBrick::HTTPUtils.escape(nil) raises a confusing exception #96
%irb
> RUBY_VERSION
=> "2.7.6"
> WEBrick::HTTPUtils.escape(nil)
Traceback (most recent call last):
7: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/bin/irb:23:in `<main>'
6: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/bin/irb:23:in `load'
5: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
4: from (irb):2
3: from (irb):3:in `rescue in irb_binding'
2: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/lib/ruby/2.7.0/webrick/httputils.rb:467:in `escape'
1: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/lib/ruby/2.7.0/webrick/httputils.rb:443:in `_escape'
NoMethodError (undefined method `b' for nil:NilClass)
WEBrick::HTTPUtils.escape_form(nil) is also the same. If the exception was raised in complex situations (e.g. in third party library), it will be difficult to determine the cause (parameter is nil).
My suggestions are following.
Raises a builtin exception
CGI.escape is this behavior (raises a TypeError)
Raises a custom exception
Like a EscapeError
Returns ""
URI.encode_www_form_component and ERB::Util.url_encode are this behavior
Indeed this is confusing and not nice to handle for 3rd party code.
re 1) AFAIK CGI.escape raising a TypeError (with no implicit conversion of nil into String) is not necessarily meant as input validation check, it just fails as part of its internal workings.
re 3) While just retuning nil or "" could be something for a new API, I think that might be a bad idea for WEBrick today.
I personally would suggest raising an ArgumentError in this case.
Activity
tisba commentedon Nov 26, 2024
Indeed this is confusing and not nice to handle for 3rd party code.
re 1) AFAIK
CGI.escaperaising aTypeError(withno implicit conversion of nil into String) is not necessarily meant as input validation check, it just fails as part of its internal workings.re 3) While just retuning
nilor""could be something for a new API, I think that might be a bad idea for WEBrick today.I personally would suggest raising an
ArgumentErrorin this case.