"rescue Postgres::PGError" not working

M

Mandeep Baruah

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?
 
B

Brian Candler

Mandeep said:
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.
 
E

Erik Hollensbe

Mandeep said:
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
 
M

Mandeep Baruah

Erik said:
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
(http://www.troubleshooters.com/codecorn/ruby/database/index.htm).
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top