"rescue Postgres::PGError" not working

I am trying to rescue exceptions from a PostgreSQL query (see bellow
code)

begin
res = @pgconn.exec(querystring) # some query
rescue Postgres::PGError => e
puts “Error code: #{e.err}”
puts “Error message: #{e.errstr}”
end

(I know that the query I am tring to execute will give an exception)
But when I try to execute, it give me an error saying:
“dependencies.rb:493:in `const_missing’: uninitialized constant
UpdateTestlinkDB::Postgres (NameError)”

Is there a different way to handle exception or, am I missing something?

Mandeep Baruah wrote:

I am trying to rescue exceptions from a PostgreSQL query (see bellow
code)

begin
res = @pgconn.exec(querystring) # some query
rescue Postgres::PGError => e
puts “Error code: #{e.err}”
puts “Error message: #{e.errstr}”
end

(I know that the query I am tring to execute will give an exception)
But when I try to execute, it give me an error saying:
“dependencies.rb:493:in `const_missing’: uninitialized constant
UpdateTestlinkDB::Postgres (NameError)”

Is there a different way to handle exception or, am I missing something?

The exception you are rescuing is not the one you actualyl got. Here the
exception is NameError, which is not a subclass of Postgres::PGError, so
it falls through the rescue clause.

You could rescue a NameError in a separate rescue clause. Or you could
rescue StandardError (common run-time exceptions) or Exception (all
exceptions).

However I’m pretty sure that’s not what you want here. Ruby is telling
you that line 493 of dependencies.rb is trying to access a constant
‘Postgres’ which does not exist at this time. Perhaps this means you
didn’t require the postgres library before this code was run.

So it’s more fundamental than Postgres not liking the SQL you sent to
it; it’s that it couldn’t even send the SQL in the first place because
the library isn’t available.

Mandeep Baruah wrote:

I am trying to rescue exceptions from a PostgreSQL query (see bellow
code)

begin
res = @pgconn.exec(querystring) # some query
rescue Postgres::PGError => e
puts “Error code: #{e.err}”
puts “Error message: #{e.errstr}”
end

(I know that the query I am tring to execute will give an exception)
But when I try to execute, it give me an error saying:
“dependencies.rb:493:in `const_missing’: uninitialized constant
UpdateTestlinkDB::Postgres (NameError)”

Is there a different way to handle exception or, am I missing something?

I’m fairly certain (unless it’s changed, then I have some code to push)
that the class is PGError, not Postgres::PGError.

This would explain your error, as ruby will first search the current
namespace, then the global namespace, looking for your constant
(Postgres in this case), and will report it missing.

-Erik

Erik H. wrote:

Mandeep Baruah wrote:

I am trying to rescue exceptions from a PostgreSQL query (see bellow
code)

begin
res = @pgconn.exec(querystring) # some query
rescue Postgres::PGError => e
puts “Error code: #{e.err}”
puts “Error message: #{e.errstr}”
end

(I know that the query I am tring to execute will give an exception)
But when I try to execute, it give me an error saying:
“dependencies.rb:493:in `const_missing’: uninitialized constant
UpdateTestlinkDB::Postgres (NameError)”

Is there a different way to handle exception or, am I missing something?

I’m fairly certain (unless it’s changed, then I have some code to push)
that the class is PGError, not Postgres::PGError.

This would explain your error, as ruby will first search the current
namespace, then the global namespace, looking for your constant
(Postgres in this case), and will report it missing.

-Erik

I did try only with PGError, but that too does not work. I was following
the steps mentioned in here
(Ruby DBI: Database Access).
Finally I have used ‘StandardError’ successfully to rescue the
exception. Seems to be working now!.
begin
res = @pgconn.exec(querystring) # some query
rescue StandardError => e
puts e
end

Thanks!. Guys

Mandeep Baruah wrote:

(Ruby DBI: Database Access).
Finally I have used ‘StandardError’ successfully to rescue the
exception. Seems to be working now!.
begin
res = @pgconn.exec(querystring) # some query
rescue StandardError => e
puts e
end

puts e.class.ancestors.inspect should give you a list of possible
more-specific exceptions to capture.