"rescue Postgres::PGError" not working

Discussion in 'Ruby' started by Mandeep Baruah, Nov 19, 2008.

  1. 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?
    --
    Posted via http://www.ruby-forum.com/.
     
    Mandeep Baruah, Nov 19, 2008
    #1
    1. Advertising

  2. 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Nov 19, 2008
    #2
    1. Advertising

  3. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Erik Hollensbe, Nov 19, 2008
    #3
  4. Erik Hollensbe 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/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
    --
    Posted via http://www.ruby-forum.com/.
     
    Mandeep Baruah, Nov 19, 2008
    #4
  5. Brian Candler, Nov 19, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ratman
    Replies:
    0
    Views:
    679
    Ratman
    Sep 14, 2004
  2. Martin Heuckeroth
    Replies:
    5
    Views:
    693
    JiangZemin
    Apr 1, 2005
  3. Alan Silver
    Replies:
    1
    Views:
    3,793
    Alan Silver
    Aug 2, 2005
  4. gaurav tyagi
    Replies:
    14
    Views:
    1,350
    gaurav tyagi
    Jan 20, 2006
  5. Priyanka AGARWAL
    Replies:
    9
    Views:
    10,032
    Gordon Beaton
    May 25, 2004
Loading...

Share This Page