rake test and error : log writing failed. “\xE2” from ASCII-8BIT to UTF-8

你说的曾经没有我的故事 提交于 2019-12-21 07:10:57

问题


When I run rake test, I get error line every time.

log writing failed. "\xE2" from ASCII-8BIT to UTF-8  

Please guide on this. How to remove this error.


回答1:


You probably have somewhere in your code trying to output string whose encoding is wrong or has some weird characters and the encoding doesn't work.

Correct way would be to find the string that results in problems and find out why its in wrong encoding. See encoding and force_encoding in string api (http://ruby-doc.org/core-2.2.0/String.html).

If you just want to be able to print that line in the log and avoid the error you can do the following procedure with that string.

str = 'here is some string with wrong encoding and funny characters e.g. "\xE2"'
# note if you do this in console, the str is encoded correctly

# Rails.logger.info(str) # this would result in the error line

# This will change the encoding and remove weird characters
str = str.encode('utf-8', :invalid => :replace, :undef => :replace, :replace => '_')

Rails.logger.info(str) # this now works



回答2:


Are you using postgresql? Your database might be using SQL_ASCII. You can check if it is by running psql template1 -c 'show server_encoding' Delete the database cluster and reinitialize it with initdb -E utf8 /path/to/database.




回答3:


In my case some weird quotation marks were the culprit so I just replaced them with the safe characters like this,

msg = error.message.gsub(/[“”]/, "\"").gsub(/[‘’]/,"\'")


来源:https://stackoverflow.com/questions/13821258/rake-test-and-error-log-writing-failed-xe2-from-ascii-8bit-to-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!