Univention Bugzilla – Full Text Bug Listing |
Description
Florian Best
2013-10-25 10:05:41 CEST
<http://stackoverflow.com/questions/4825234/exception-traceback-is-hidden-if-not-re-raised-immediately> <http://docs.python.org/2/reference/simple_stmts.html#grammar-token-raise_stmt> #!/usr/bin/python import sys import traceback def foo(): raise KeyError("foo") def bar(): try: foo() except KeyError as ex: print "bar.type=%s" % (type(ex),) print "bar.ex=%s" % (ex,) info = sys.exc_info() msg = "foo() failed in context 'bar': %s" % (info[1],) raise IndexError, msg, info[2] def main(): try: bar() except (KeyError, LookupError) as ex: print "main.type=%s" % (type(ex),) print "main.ex=%s" % (ex,) traceback.print_exc() if __name__ == "__main__": main() bar.type=<type 'exceptions.KeyError'> bar.ex='foo' main.type=<type 'exceptions.IndexError'> main.ex=foo() failed in context 'bar': 'foo' Traceback (most recent call last): File "/tmp/exception.py", line 20, in main bar() File "/tmp/exception.py", line 10, in bar foo() File "/tmp/exception.py", line 6, in foo raise KeyError("foo") IndexError: foo() failed in context 'bar': 'foo' This is especially a problem with the LDAP_Connection decorator (which is used everywhere) as it can mask important traceback information. Created attachment 5812 [details]
cleanup LDAP_Connection decorator
Bug#34436 is an example of a traceback which does not really provide enough information. This Bug should be fixed. Again received a feedback where the exception is reraised without the traceback. We received again a traceback which doesn't offer any relevant information. The two biggest problems were the LDAP_ConnectionError exception and if a function which already uses @LDAP_Connection calls another function which uses the decorator, too. * The LDAP_ConnectionError was removed as it hides every exception detail which is required to reproduce a problem when the initialization of the module fails. * No exception is reraised with 'raise e' anymore. * Even UDM_Error exceptions which result in a "user friendly" error message preserve the original traceback of the last raised exception. * A error_handling() function has been implemented to separate error handling from LDAP actions * If an LDAP error (not UDM error) occurs the current opened LDAP connection is thrown away. On the next LDAP action a new one is created. * Tracebacks are now generated in the same way as in the UMC core (1*) * Logging of exceptions has been improved Fix: svn r56420, svn r56478 Package: univention-management-console-module-udm (5.1.25-5) YAML: 2014-11-25-univention-management-console-module-udm.yaml *1) msg1 = before, msg2 = after >>> msg1 = '%s\n%s: %s\n' % (''.join(traceback.format_tb(thread.exc_info[2])), thread.exc_info[0].__name__, str(thread.exc_info[1])) >>> msg2 = ''.join(traceback.format_exception(*thread.exc_info)) >>> >>> msg1 ' File "<stdin>", line 2, in <module>\n\nNameError: name \'asdf\' is not defined\n' >>> msg2 'Traceback (most recent call last):\n File "<stdin>", line 2, in <module>\nNameError: name \'asdf\' is not defined\n' Works. Much better. |