add backtraces to exceptions

This commit is contained in:
Christophe Benz 2010-05-13 22:52:34 +02:00
commit 5bdc7605e5

View file

@ -1,22 +1,19 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" # Copyright(C) 2010 Romain Bignon, Christophe Benz
Copyright(C) 2010 Romain Bignon #
# This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License.
the Free Software Foundation, version 3 of the License. #
# This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details.
GNU General Public License for more details. #
# You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
from __future__ import with_statement from __future__ import with_statement
@ -29,7 +26,8 @@ __all__ = ['BackendsCall', 'CallErrors']
class CallErrors(Exception): class CallErrors(Exception):
def __init__(self, errors): def __init__(self, errors):
Exception.__init__(self, u"Several errors have been raised:\n%s" % (u'\n'.join((u'%s: %s' % (b, e)) for b, e in errors))) Exception.__init__(self, u'These errors have been raised in backend threads:\n%s' % (
u'\n'.join((u' * %s: %s\n%s' % (backend, error, backtrace)) for backend, error, backtrace in errors)))
self.errors = copy(errors) self.errors = copy(errors)
def __iter__(self): def __iter__(self):
@ -76,53 +74,53 @@ class BackendsCall(object):
# Create jobs for each backend # Create jobs for each backend
with self.mutex: with self.mutex:
for b in backends: for backend in backends:
debug('New timer for %s' % b) debug('Creating a new thread for %s' % backend)
self.threads.append(Timer(0, self._caller, (b, function, args, kwargs)).start()) self.threads.append(Timer(0, self._caller, (backend, function, args, kwargs)).start())
if not backends: if not backends:
self.finish_event.set() self.finish_event.set()
def _store_error(self, b, e): def _store_error(self, backend, error):
with self.mutex: with self.mutex:
# TODO save backtrace and/or print it here (with debug) backtrace = get_backtrace(error)
self.errors.append((b, e)) self.errors.append((backend, error, backtrace))
debug(get_backtrace(e))
def _store_result(self, b, r): def _store_result(self, backend, result):
with self.mutex: with self.mutex:
self.responses.append((b,r)) self.responses.append((backend, result))
self.response_event.set() self.response_event.set()
def _caller(self, b, function, args, kwargs): def _caller(self, backend, function, args, kwargs):
debug('Hello from timer %s' % b) debug('%s: Thread created successfully' % backend)
with b: with backend:
try: try:
# Call method on backend # Call method on backend
try: try:
debug('%s: Calling function %s' % (backend, function))
if callable(function): if callable(function):
r = function(b, *args, **kwargs) result = function(backend, *args, **kwargs)
else: else:
r = getattr(b, function)(*args, **kwargs) result = getattr(backend, function)(*args, **kwargs)
except Exception, e: except Exception, error:
self._store_error(b, e) self._store_error(backend, error)
else: else:
debug('%s: Got answer! %s' % (b, r)) debug('%s: Called function %s returned: "%s"' % (backend, function, result))
if hasattr(r, '__iter__'): if hasattr(result, '__iter__'):
# Loop on iterator # Loop on iterator
try: try:
for e in r: for subresult in result:
# Lock mutex only in loop in case the iterator is slow # Lock mutex only in loop in case the iterator is slow
# (for example if backend do some parsing operations) # (for example if backend do some parsing operations)
self._store_result(b, e) self._store_result(backend, subresult)
except Exception, e: except Exception, error:
self._store_error(b, e) self._store_error(backend, error)
else: else:
self._store_result(b, r) self._store_result(backend, result)
finally: finally:
with self.mutex: with self.mutex:
# This backend is now finished # This backend is now finished
self.backends[b.name] = True self.backends[backend.name] = True
for finished in self.backends.itervalues(): for finished in self.backends.itervalues():
if not finished: if not finished:
return return