1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#
# Dblatex Error Handler wrapper providing:
# - The ErrorHandler class definition, that must be the parent of any actual
# error handler.
# - A general API.
#
from __future__ import print_function
import sys
import traceback
class ErrorHandler(object):
"""
Object in charge to handle any error occured during the dblatex
transformation process. The first mandatory argument is the <object>
that signaled the error.
"""
def __init__(self):
pass
def signal(self, object, *args, **kwargs):
failure_track("Unexpected error occured")
_current_handler = None
_dump_stack = False
#
# Dblatex Error Handler API
#
# In a complex use of the API, a locking mechanism (thread.lock) should
# be used. The current implementation assumes that setup is done before
# any get().
#
def get_errhandler():
global _current_handler
# If nothing set, use a default handler that does nothing
if not(_current_handler):
_current_handler = ErrorHandler()
return _current_handler
def set_errhandler(handler):
global _current_handler
if not(isinstance(handler, ErrorHandler)):
raise ValueError("%s is not an ErrorHandler" % handler)
_current_handler = handler
def signal_error(*args, **kwargs):
get_errhandler().signal(*args, **kwargs)
def failure_track(msg):
global _dump_stack
print((msg), file=sys.stderr)
if _dump_stack:
traceback.print_exc()
def failed_exit(msg, rc=1):
failure_track(msg)
sys.exit(rc)
def dump_stack():
global _dump_stack
_dump_stack = True
|