Rescuing ActiveRecord errors without logging


I have the following piece of code:




if $!.class == ActiveRecord::StatementInvalid and $!.to_s[‘PG::CharacterNotInRepertoire’]“Issues with encoding for #{record[:email_domain]}. Attempting encoding”)



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 = [])


:sql => sql,
:name => name,
:connection_id => object_id,
:binds => binds) { yield }
rescue => e
message = “#{}: #{e.message}: #{sql}”
@logger.error message if @logger
exception = translate_exception(e, message)
exception.set_backtrace e.backtrace
raise exception

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.

Hi there , i don't know if that will help you , but i did have this kind
of encoding problem with my database few weeks ago.

I solved it by running this query on my DB:

update database set encoding = char_to_encoding('UTF8') where datname =

Any advice would be highly appreciated.

This is one , maybe not your problem's solution :confused:

see you

ps:English is not my mother tongue

I’m not speaking about the encoding issue but about the exception rescue issue.
Imagine I would like to handle unique key exceptions for instance.