0

When I do

conn = PGconn.connect("localhost", 5432, "" , "", db_name, postgres_username, postgres_password)

and the password is incorrect, an exception (PG::ConnectionBad) is thrown (which is what I expect)

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

when I do a "puts e" I get

FATAL:  password authentication failed for user "something@something.com"
FATAL:  password authentication failed for user "something@something.com"

That is, the failure message is repeated.

I'm using

  • PostgreSQL 9.6.3 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609, 64-bit

    ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

    Ubuntu 16.04

Is this a bug? Is it to be expected?

RalphShnelvar
  • 557
  • 1
  • 6
  • 17

1 Answers1

0

The following is a hack and does not get to the root of the question. It just cures the symptom of the problem.

The "answer" is deliberately verbose.

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
RalphShnelvar
  • 557
  • 1
  • 6
  • 17