Error "errorType": "Function<Encoding::UndefinedConversionError>" on marshall_response


#1

Rails API mode.

I’m from Brazil. Our language is Portuguese, where it contains accents.

When the response contains accents (é, ê and ã), the following error occurs in Jets:

How to configure to utf-8?

I, [2019-01-20T00:07:36.938338 #8] INFO -- : Completed Status Code 200 in 0.085300454s
Error raised from handler method
{
"errorMessage": "\"\\xC3\" from ASCII-8BIT to UTF-8",
"errorType": "Function<Encoding::UndefinedConversionError>",
"stackTrace": [
"/opt/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/core_ext/object/json.rb:38:in `encode'",
"/opt/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/core_ext/object/json.rb:38:in `to_json'",
"/opt/ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/core_ext/object/json.rb:38:in `to_json'",
"/var/lang/lib/ruby/2.5.0/json/common.rb:224:in `generate'",
"/var/lang/lib/ruby/2.5.0/json/common.rb:224:in `generate'",
"/var/lang/lib/ruby/2.5.0/json/common.rb:394:in `dump'",
"/opt/ruby/gems/2.5.0/gems/jets-1.5.3/lib/jets/overrides/lambda/marshaller.rb:25:in `marshall_response'"
]
}
END RequestId: c0f84734-da10-4c01-ac6b-07911d9a852a
REPORT RequestId: c0f84734-da10-4c01-ac6b-07911d9a852a	Duration: 89.57 ms	Billed Duration: 100 ms Memory Size: 1536 MB	Max Memory Used: 404 MB

#2

Played in irb and trying to figure out how to create an ASCII_8BIT string so can test this appropriately.

$ irb
>> Encoding.default_internal = Encoding.default_external = Encoding::ASCII_8BIT
=> #<Encoding:ASCII-8BIT>
>> io = StringIO.new("é, ê and ã")
=> #<StringIO:0x00005642b6b9ccc0>
>> str = io.read
=> "\u00E9, \u00EA and \u00E3"
>> str.encoding
=> #<Encoding:UTF-8>
>> 

Hoping for a test project to reproduce this and hopefully figure this out.


#3

Hi! @tung!

You can reproduce with the sample project rails-api retuning a json with accents in any action of controller.


#4

Did I identify that Jets is replacing the file database.yml of my Rails project for one default file.

default: &default
  adapter:  mysql2
  encoding: utf8
  pool: <%= ENV["DB_POOL"] || 5  %>
  database: <%= ENV['DB_NAME'] || 'mk_sv_development' %>
  username: <%= ENV['DB_USER'] || 'root' %>
  password: <%= ENV['DB_PASS'] %>
  host: <%= ENV["DB_HOST"] %>
  url: <%= ENV['DATABASE_URL'] %> # takes higher precedence than other settings

development:
  <<: *default
  database: <%= ENV['DB_NAME'] || 'mk_sv_development' %>

test:
  <<: *default
  database: mk_sv_test

production:
  <<: *default
  database: mk_sv_production
  url: <%= ENV['DATABASE_URL'] %>

#5

I imagine it is not using the utf8 encoding, as I set the DATABASE_URL in the environment variable. Certainly that is the problem.


#6

Okay. Added it to the demo-rails-api project to reproduce.

Kind of, a rack/config/database.yml gets generated if one is not available. You can create your own in the rails_project/.jets/app/config/database.yml and that one would be used instead if it exists.

Don’t belive that is the issue. It has to do with the response returned from the net/http response. There are mixed opinions about how to handle this generally for the net/http library: https://bugs.ruby-lang.org/issues/2567 For jets, think it is fine to respect the encoding provided in the content-header though. Fix is here: https://github.com/tongueroo/jets/pull/164 Will go out in the next version, after some QA and testing.