Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Created August 31, 2011 19:54
Show Gist options
  • Select an option

  • Save coldnebo/1184533 to your computer and use it in GitHub Desktop.

Select an option

Save coldnebo/1184533 to your computer and use it in GitHub Desktop.
[FIXED] ruby encodings... can we word this error differently?
$ irb
ruby-1.9.2-p180 :001 > "“"
=> "“"
ruby-1.9.2-p180 :002 > "“".encoding
=> #<Encoding:UTF-8>
ruby-1.9.2-p180 :003 > "\“"
SyntaxError: (irb):7: invalid multibyte char (UTF-8)
(irb):7: invalid multibyte char (UTF-8)
from /local/rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
@coldnebo

Copy link
Copy Markdown
Author

So, maybe a wee bit pedantic, but is a completely valid multibyte UTF-8 char...

A better error would have been SyntaxError: (irb):7: invalid escape sequence: '\“'.

Note: since this error must be handled within the parser at a slightly different place than the usual string encoding checks it should be relatively easy to add this context.

@coldnebo

Copy link
Copy Markdown
Author

The original error context popped up in a rails view and was much harder to get any meaning from. Rails could have left in the caller() context from the original failsafe exception to help track things down, but the fault lies in multiple layers of parsing and exception transformation:

link_to("available from the \“Help\” link", "https://help", :class => "std_link")

Error during failsafe response: "\xE2" from ASCII-8BIT to UTF-8
[2011-08-31 15:18:08] ERROR NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
      /local/rvm/gems/ruby-1.9.2-p180/gems/rack-1.1.2/lib/rack/utils.rb:267:in `initialize'
      /local/rvm/gems/ruby-1.9.2-p180/gems/rack-1.1.2/lib/rack/utils.rb:261:in `new'
      /local/rvm/gems/ruby-1.9.2-p180/gems/rack-1.1.2/lib/rack/utils.rb:261:in `new'
      /local/rvm/gems/ruby-1.9.2-p180/gems/rack-1.1.2/lib/rack/content_length.rb:14:in `call'
      /local/rvm/gems/ruby-1.9.2-p180/gems/rack-1.1.2/lib/rack/handler/webrick.rb:48:in `service'
      /local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
      /local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
      /local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

@coldnebo

coldnebo commented Sep 1, 2011

Copy link
Copy Markdown
Author

opened an issue at ruby-core: http://redmine.ruby-lang.org/issues/5262

@coldnebo

coldnebo commented Nov 5, 2011

Copy link
Copy Markdown
Author

yay, issue is fixed in ruby 1.9.3!

@nagarajpg

Copy link
Copy Markdown

Hi,

This issue is not yet fixed ! I am getting the same error message in 1.9.3p0.

[akshatha@localhost chiliproject]$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]

[akshatha@localhost chiliproject]$ rails -v
Rails 2.3.14

Application: Chiliproject 3.0.0beta + haltr plugin

Processing InvoicesController#index (for 127.0.0.1 at 2012-01-07 00:19:57) [GET]
Parameters: {"controller"=>"invoices", "action"=>"index", "id"=>"test-project"}
Rendering template within layouts/haltr
Rendering invoices/index
Completed in 212ms (View: 76, DB: 13) | 200 OK [http://0.0.0.0/invoices/index/test-project]
Error during failsafe response: "\xE2" on US-ASCII
[2012-01-07 00:20:01] ERROR NoMethodError: undefined method each' for nil:NilClass /home/akshatha/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.1.3/lib/rack/utils.rb:324:ininitialize'
/home/akshatha/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.1.3/lib/rack/utils.rb:318:in new' /home/akshatha/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.1.3/lib/rack/utils.rb:318:innew'
/home/akshatha/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.1.3/lib/rack/content_length.rb:14:in call' /home/akshatha/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.1.3/lib/rack/handler/webrick.rb:48:inservice'
/home/akshatha/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in service' /home/akshatha/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:inrun'
/home/akshatha/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Thanks
Nagaraj

@coldnebo

coldnebo commented Jan 6, 2012

Copy link
Copy Markdown
Author

Just checked it in irb... it's fixed in Ruby at least:

ruby-1.9.3-p0 :001 > "“"
 => "“" 
ruby-1.9.3-p0 :002 > "“".encoding
 => #<Encoding:UTF-8> 
ruby-1.9.3-p0 :003 > "\“"
 => "“" 

What do you get if you use irb as above?

@nagarajpg

nagarajpg commented Jan 7, 2012 via email

Copy link
Copy Markdown

@coldnebo

coldnebo commented Jan 9, 2012

Copy link
Copy Markdown
Author

Hi Nagaraj, your irb session shows that the underlying problem with escaped UTF-8 has been fixed as I originally reported it. The higher level error you are seeing "Error during failsafe" is a rack or Rails error in your application and likely has a different root cause.

@VenkatesanPrabhu

Copy link
Copy Markdown

Good one..Clear explanation..Thanks to the author..
i've wrote an article on the RUBY INVALID MULTI BYTE!
http://www.wikitechy.com/fix-error/ruby-invalid-multi-byte-character
Please visit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment