Nice posting to keep in mind the next time you touch something within python :)

Cliff

-------- Original Message --------
Subject: [Pulp-list] Exception Handling
Date: Wed, 18 May 2011 15:43:20 -0600
From: Jason L Connor <jcon...@redhat.com>
Organization: Red Hat, Inc.
To: pulp-list <pulp-l...@redhat.com>

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

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Pulp-list mailing list
pulp-l...@redhat.com
https://www.redhat.com/mailman/listinfo/pulp-list

_______________________________________________
Spacewalk-devel mailing list
Spacewalk-devel@redhat.com
https://www.redhat.com/mailman/listinfo/spacewalk-devel

Reply via email to