Rescuing ActiveRecord errors without logging

61 views
Skip to first unread message

Inba

unread,
Apr 19, 2015, 8:58:10 AM4/19/15
to rubyonra...@googlegroups.com
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.

Simioni Tanguy

unread,
Apr 20, 2015, 10:53:05 AM4/20/15
to rubyonra...@googlegroups.com
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 =
'thedb'

>>Any advice would be highly appreciated.

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

see you

Tanguy
ps:English is not my mother tongue

--
Posted via http://www.ruby-forum.com/.

Inba

unread,
Apr 21, 2015, 9:28:56 AM4/21/15
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages