[Pulp-list] Exception Handling

Jason L Connor jconnor at redhat.com
Wed May 18 21:43:20 UTC 2011


Hi All,

I need to gripe publicly a bit here about some of our exception handling
in pulp.

In a nutshell, NEVER NEVER NEVER DO THIS:

try:
    .... # some work
except:
    ... # possibly something else that doesn't include logging the traceback
    raise MyOwnExceptionClass('new message here')

We've pointed this out before, and I'm pointing it out again. THIS MASKS
THE ORIGINAL ERROR! When trying to debug these, I usually feel like
strangling someone.

We should either (1) log the current exception and traceback if we're
actually handling the error (e.g. at the top level of the web services
or in the pulp client), (2) re-raise the original error (e.g. useful for
doing some local cleanup), or (3) concatenate the existing traceback to
our new exception (e.g. when differentiating the error condition from an
underlying standard library or third party exception).

Let me demonstrate the last two.


Re-raising the original error (2):

try:
    .... # some work
except:
    .... # local clean up, logging, etc.
    raise

The last empty 'raise' statement will conveniently re-raise the caught
exception.


Concatenating the existing traceback (3):

import sys

try:
    ... # some work
except:
    ... # optional clean up, etc.
    raise PulpException('more meaningful error message'), None, sys.exc_info()[2]

The ', None, sys.exc_info()[2]' portion at the end of the new 'raise'
statement will concatenate the exiting traceback to the traceback
generated by the raise. This allows us to see the original point where
something went wrong instead of hiding it beneath the new PulpException.

Note: it can be any exception, PulpException is just used as an example.


I'm not expecting us to fix all of the code now. However, if you're
working and spot one of these, please take the time to fix it. Also, if
I catch you checking one of these in, in new code, the next email *will*
mention your name.

Thank you. That is all.

-- 
Jason L Connor
linear on freenode #pulp
http://pulpproject.org/
RHCE: 805010912355231
GPG Fingerprint: 2048R/CC4ED7C1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part
URL: <http://listman.redhat.com/archives/pulp-list/attachments/20110518/a2e4f725/attachment.sig>


More information about the Pulp-list mailing list