Hi,
I have the following piece of code:
begin
DummyModel.create(record)
rescue
if $!.class == ActiveRecord::StatementInvalid and $!.to_s[‘PG::CharacterNotInRepertoire’]
Logger.info(“Issues with encoding for #{record[:email_domain]}. Attempting encoding”)
DummyModel.create(Utils.encode_record_as_utf8(record))
end
Since ActiveRecord always sends errors to logger I always see the error in my log files, even tough I rescue it and handle it.
I see this error in the log files:
Apr 19 10:43:16 E, [2015-04-19T07:43:16.217488 #1] ERROR – : PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding “UTF8”: 0xf1 0x6f 0x0a 0x52
I’m running rails 4.0.1, but upgrade to rails 4.2 didn’t help.
This is very problematic, as I cannot monitor my log for errors - the monitor always sends me this error, even though it is already taken care of.
I found out the piece of rails code responsible for this behavior inside AbstractAdapter.log method
def log(sql, name = “SQL”, binds = )
@instrumenter.instrument(
“sql.active_record”, :sql => sql, :name => name, :connection_id => object_id, :binds => binds) { yield } rescue => e message = “#{e.class.name}: #{e.message}: #{sql}” @logger.error message if @logger exception = translate_exception(e, message) exception.set_backtrace e.backtrace raise exception end
As you can see, error logging cannot be avoided (unless logger is disabled)
Can anyone think of a way around this?
Any advice would be highly appreciated.