Katello/katello — pull request #797 by xsuchy (2012-10-05 12:27:22+00:00)
Title: do not mask original error by raise in exception

From Jason email:
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.