Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra/Ruby - Internal Server Error: invalid byte sequence in US-ASCII. Caused by array with special characters

I am using Sinatra to build an app. The app runs just fine except for when I recently added an element to an array that has special characters (Yóü).

@peeps = ["Joe", "James", "Phil", "Jane", "Yóü"]

I use this array for a number of things, though I'm not sure I need to include them in this post. When I run my app, I get the following eror in my browser:

Internal Server Error: invalid byte sequence in US-ASCII

In my terminal window, I see the following after I run ruby app.rb

app.rb:34: invalid multibyte char (US-ASCII)
app.rb:34: invalid multibyte char (US-ASCII)
app.rb:34: syntax error, unexpected $end, expecting ']'

Also, for what it's worth, I typically use shotgun to run my server since I can make changes and just refresh the page. That is displaying this error when I run shotgun app.rb

[2012-08-16 21:36:37] ERROR ArgumentError: invalid byte sequence in US-ASCII
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/rack-1.4.1/lib/rack/utils.rb:182:in `gsub'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/rack-1.4.1/lib/rack/utils.rb:182:in `escape_html'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/shotgun-0.9/lib/shotgun/loader.rb:76:in `format_error'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/shotgun-0.9/lib/shotgun/loader.rb:52:in `proceed_as_parent'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/shotgun-0.9/lib/shotgun/loader.rb:28:in `call!'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/shotgun-0.9/lib/shotgun/static.rb:14:in `call'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
    /Users/me/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
    /Users/me/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /Users/me/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /Users/me/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
like image 531
tonic Avatar asked Nov 23 '25 16:11

tonic


1 Answers

What finally did the trick for me was adding the following line at the top of config.ru:

Encoding.default_external = Encoding::UTF_8

Hope it helps.

like image 197
José Fernandes Avatar answered Nov 25 '25 09:11

José Fernandes