Forum: Ruby "rescue Postgres::PGError" not working

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mandeep B. (Guest)
on 2008-11-19 11:16
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?
Brian C. (Guest)
on 2008-11-19 12:03
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.
Erik H. (Guest)
on 2008-11-19 12:32
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
Mandeep B. (Guest)
on 2008-11-19 14:55
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
(http://www.troubleshooters.com/codecorn/ruby/datab...).
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
Brian C. (Guest)
on 2008-11-19 16:40
Mandeep Baruah wrote:
> (http://www.troubleshooters.com/codecorn/ruby/datab...).
> 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.
This topic is locked and can not be replied to.