当我这样做时
conn = PGconn.connect("localhost", 5432, "" , "", db_name, postgres_username, postgres_password)
并且密码不正确,抛出异常(PG :: ConnectionBad)(这是我的期望)
begin
conn = PGconn.connect("localhost", 5432, "" , "", db_name, postgres_username, postgres_password)
# Do some stuff
rescue PG::ConnectionBad => e
puts e
raise # reraise exception
rescue Exception => e
puts e
raise # reraise exception
ensure
conn.close if conn
end
当我做“put e”时,我得到了
FATAL: password authentication failed for user "something@something.com"
FATAL: password authentication failed for user "something@something.com"
即,重复失败消息。
我正在使用
x86_64-pc-linux-gnu上的PostgreSQL 9.6.3,由gcc编译(Ubuntu 5.4.0-6ubuntu1~16.04.4)5.4.0 20160609,64位
ruby 2.4.1p111(2017-03-22修订版58053)[x86_64-linux]
Ubuntu 16.04
这是一个错误吗?这是预期的吗?
答案 0 :(得分:0)
以下是一个黑客,并没有找到问题的根源。它只是解决了问题的症状。
“回答”故意冗长。
begin
conn = PGconn.connect("localhost", 5432, "" , "", db_name, postgres_username, postgres_password)
# Do some stuff
rescue PG::ConnectionBad => e
# There seems to be a bug in PG::ConnectionBad where the error message is doubled. Get only a single error message
puts e
theDoubledMsg = e.to_s
theSingleMsg = theDoubledMsg.split("\n")[0]
# See https://stackoverflow.com/questions/2823748/how-do-i-add-information-to-an-exception-message-in-ruby
raise $!, theSingleMsg, $!.backtrace
rescue Exception => e
puts e
raise # reraise exception
ensure
conn.close if conn
end