Commit 8ba522a3 authored by Philip ABBET's avatar Philip ABBET

Improve the exception handling

parent 7b9b4bac
......@@ -130,8 +130,11 @@ class MessageHandler(gevent.Greenlet):
self.stop.set()
break
except RuntimeError as e:
self.send_error(str(e), kind='usr')
self.user_error = str(e)
import traceback
message = traceback.format_exc()
logger.error(message, exc_info=True)
self.send_error(message, kind='usr')
self.user_error = message
if self.process is not None:
self.process.kill()
self.stop.set()
......
......@@ -69,6 +69,9 @@ class UserError(Exception):
return repr(self.value)
#----------------------------------------------------------
def send_error(logger, socket, tp, message):
"""Sends a user (usr) or system (sys) error message to the infrastructure"""
......@@ -98,6 +101,27 @@ def send_error(logger, socket, tp, message):
logger.error('stopping 0MQ client anyway')
#----------------------------------------------------------
def process_traceback(tb, prefix):
import traceback
databases_prefix = os.path.join(prefix, 'databases') + os.sep
for first_line, line in enumerate(tb):
if line[0].startswith(databases_prefix):
break
s = ''.join(traceback.format_list(tb[first_line:]))
s = s.replace(databases_prefix, 'databases' + os.sep).strip()
return s
#----------------------------------------------------------
def main():
package = __name__.rsplit('.', 2)[0]
version = package + ' v' + \
......@@ -185,8 +209,7 @@ def main():
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = ''.join(traceback.format_list(tb[4:])) #exclude this file
s = s.replace(args['<dir>'], '').strip()
s = process_traceback(tb, os.pth.join(args['<dir>'], 'prefix'))
raise UserError("%s%s: %s" % (s, type(e).__name__, e))
# Execute the code
......@@ -200,8 +223,7 @@ def main():
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = ''.join(traceback.format_list(tb[4:]))
s = s.replace(args['<dir>'], '').strip()
s = process_traceback(tb, os.pth.join(args['<dir>'], 'prefix'))
raise UserError("%s%s: %s" % (s, type(e).__name__, e))
except UserError as e:
......
......@@ -107,10 +107,28 @@ def send_error(logger, socket, tp, message):
def close(logger, socket, context):
socket.setsockopt(zmq.LINGER, 0)
socket.close()
context.term()
logger.debug("0MQ client finished")
socket.setsockopt(zmq.LINGER, 0)
socket.close()
context.term()
logger.debug("0MQ client finished")
#----------------------------------------------------------
def process_traceback(tb, prefix):
import traceback
algorithms_prefix = os.path.join(prefix, 'algorithms') + os.sep
for first_line, line in enumerate(tb):
if line[0].startswith(algorithms_prefix):
break
s = ''.join(traceback.format_list(tb[first_line:]))
s = s.replace(algorithms_prefix, '').strip()
return s
#----------------------------------------------------------
......@@ -209,8 +227,7 @@ def main():
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = ''.join(traceback.format_list(tb[4:])) #exclude this file
s = s.replace(os.path.join(executor.prefix, 'algorithms') + os.sep, '').strip()
s = process_traceback(tb, executor.prefix)
raise UserError("%s%s: %s" % (s, type(e).__name__, e))
# Execute the code
......@@ -224,8 +241,7 @@ def main():
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = ''.join(traceback.format_list(tb[4:]))
s = s.replace(os.path.join(executor.prefix, 'algorithms') + os.sep, '').lstrip()
s = process_traceback(tb, executor.prefix)
raise UserError("%s%s: %s" % (s, type(e).__name__, e))
except UserError as e:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment