tonic
tonic

Reputation: 453

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'

Upvotes: 2

Views: 3209

Answers (3)

Slava V
Slava V

Reputation: 17256

I've had a single-file app with # encoding: utf-8 already at the top and that error still was there.

What helped me was to create config.ru file:

require File.expand_path '../my_app.rb', __FILE__
run Sinatra::Application

Upvotes: 0

muffinista
muffinista

Reputation: 6736

You haven't posted all your code, but you probably need to mark your file as being UTF-8, which you can do with a 'magic comment' at the top:

# encoding: utf-8

Here's a good explanation of this issue.

Upvotes: 4

José Fernandes
José Fernandes

Reputation: 394

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.

Upvotes: 7

Related Questions