【问题标题】:PGconn.connect exception repeats error message twicePGconn.connect 异常重复错误信息两次
【发布时间】:2018-08-23 11:48:51
【问题描述】:

当我这样做时

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

当我执行“puts 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

这是一个错误吗?可以期待吗?

【问题讨论】:

  • 编程语言是 Ruby。我很抱歉没有说清楚。

标签: ruby postgresql


【解决方案1】:

以下是一个技巧,并没有触及问题的根源。只是治标不治本。

“答案”故意冗长。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多