Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

"pgcode" should be present in exception.args

#96

Now only exception message is present in exception.args.
It would be handy if pgcode was passed to Psycopg2 exception constructor, so it would be available in exception.args - it is the purpose of agrs property to store such information.
Cheers!

python: 2.7
Psycopg2: rel-2.4.4

Reported by Piotr Czachur · January 27th, 2012 @ 10:31 AM

State: invalid
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo

    Why it would be handy? Can you make an example? Thanks.

    January 29th, 2012 @ 07:40 PM

  2. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to hold

    If we put stuff in args, it ends up being printed everywhere, changing the current behaviour of the exception:

    In [1]: class MyException(Exception): pass
       ...: 
    
    In [3]: try: raise MyException('foo', 10, 20)
       ...: except MyException, e: pass
       ...: 
    
    In [4]: e
    Out[4]: MyException('foo', 10, 20)
    
    In [5]: print e
    ('foo', 10, 20)
    

    We also have three independent attributes for exceptions: pgerror, pgcode and cursor, and the second is mostly independent from the first two, which makes positional arguments an awkward implementation.

    It's also more explicit to access exc.pgerror than exc.args[1].

    What would be the advantages in using args? Would they outweigh these drawbacks?

    January 30th, 2012 @ 01:42 PM

  3. Piotr Czachur
    Piotr Czachur

    Daniele,

    Pros:
    * When you log exception, you see also pgcode, which may be important (error messages can change, while pgcode should remain unchanged (user's stored functions))

    Cons
    * What's wrong that pgcode is being printed everywhere? * I agre that accessing e.args[1] is not so friendly, but you can always provide getter function.

    Please decide yourself... it's really not important issue.

    January 30th, 2012 @ 06:06 PM

  4. Daniele Varrazzo
    Daniele Varrazzo

    The pgcode may be important, but I suspect now it would mostly break other people code (only at visualization level, but knowing from the past I'd say it would be enough to piss people).

    My decision would be not to change the exception. However, please feel entirely free to raise the question to the mailing list and see what would be ok for other users. If there is a good use case to have the pgcode in the args, or in the str(), which I suspect is what you are really after, I would have no problem in changing the code.

    January 30th, 2012 @ 06:15 PM

  5. Piotr Czachur
    Piotr Czachur

    After some thoughts, I agree that enhancment it would provide would be supressed by backward incompatibilitiy issues, so leaving it as it is, is the best choice for now.
    If I find some more solid motivation for this change I will put on the mailing list.
    Thank you very much for great attitude.

    February 1st, 2012 @ 01:08 PM

  6. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from hold to invalid

    Closing as agreed not to change the thing.

    September 26th, 2012 @ 02:24 PM